0

大家好,我正在尝试将一些现有的代码从 VBA 转换为 VSTO,到目前为止我已经非常成功。这是我要转换的代码。

this is the vba code


Sub FindUsedRange()
Dim LastRow As Long
Dim FirstRow As Long
Dim LastCol As Integer
Dim FirstCol As Integer

' Find the FIRST real row
FirstRow = ActiveSheet.Cells.Find(What:="*", _
  SearchDirection:=xlNext, _
  SearchOrder:=xlByRows).Row

' Find the FIRST real column
FirstCol = ActiveSheet.Cells.Find(What:="*", _
  SearchDirection:=xlNext, _
  SearchOrder:=xlByColumns).Column

' Find the LAST real row
LastRow = ActiveSheet.Cells.Find(What:="*", _
  SearchDirection:=xlPrevious, _
  SearchOrder:=xlByRows).Row

' Find the LAST real column
LastCol = ActiveSheet.Cells.Find(What:="*", _
  SearchDirection:=xlPrevious, _
  SearchOrder:=xlByColumns).Column

'Select the ACTUAL Used Range as identified by the
'variables identified above
ActiveSheet.Range(Cells(FirstRow, FirstCol), _
    Cells(LastRow, LastCol)).Select
End Sub

但是当我到达这部分时

 ActiveSheet.Range(Cells(FirstRow, FirstCol), _
        Cells(LastRow, LastCol)).Select
    End Sub

在.net中我不知道如何处理?有没有人有任何想法,可以指出我正确的方向。

这是我正在构建的插件的 c# 代码,以防有人感兴趣。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
//using office.Excel;
namespace ExcelAddIn3.TaskPane
{
    public partial class TaskPaneView : UserControl
    {
        public TaskPaneView()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

var time = DateTime.Now.ToLongTimeString();

this.label1.Text = time;
System.Array arr;
Microsoft.Office.Interop.Excel.Range  LastRow;
int  firstRow;
Microsoft.Office.Interop.Excel.Range  lastCol;
Microsoft.Office.Interop.Excel.Range  firstCol;

Microsoft.Office.Interop.Excel.Range currentFind = null;
Microsoft.Office.Interop.Excel.Range firstFind = null;
Microsoft.Office.Interop.Excel.Range fruits = ExcelAddIn3.Globals.ThisAddIn.Application.get_Range("A:Z");

Microsoft.Office.Interop.Excel.Worksheet sheet;



firstRow = fruits.Find("*", Type.Missing, Type.Missing,
                                             Type.Missing,
                                             Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
                                             Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, true,
                                             Type.Missing, Type.Missing).Row;

firstCol = fruits.Find("*", Type.Missing, Type.Missing,
                                             Type.Missing,
                                             Microsoft.Office.Interop.Excel.XlSearchOrder.xlByColumns,
                                             Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, true,
                                             Type.Missing, Type.Missing);

LastRow = fruits.Find("*", Type.Missing, Type.Missing,
                                            Type.Missing,
                                             Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
                                             Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, true,
                                             Type.Missing, Type.Missing);

lastCol = fruits.Find("*", Type.Missing, Type.Missing,
                                            Type.Missing,
                                             Microsoft.Office.Interop.Excel.XlSearchOrder.xlByColumns,
                                             Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, true,
                                             Type.Missing, Type.Missing);


while (lastCol != null)
{
    if (firstFind == null)
    {
        firstFind = lastCol;
        System.Windows.Forms.MessageBox.Show(LastRow.get_Address() + "last Column.");

    }
    else
    {
        break;
    }
}


firstFind = null;          

 while (LastRow != null)
{
    if (firstFind == null)
    {
        firstFind = LastRow;
        System.Windows.Forms.MessageBox.Show(LastRow.get_Address() + "last row.");


    }
    else
    {
        break;
    }
}
firstFind = null;
while (firstCol != null)
{
    if (firstFind == null)
    {
        firstFind = firstCol;
        System.Windows.Forms.MessageBox.Show(firstCol.get_Address() + "first real column.");


    }
    else
    {
        break; 
    }
}


firstFind = null;
while(firstRow != null){
    if (firstFind == null)
    {

        System.Windows.Forms.MessageBox.Show(firstRow + "first real row.");


    }
    else 
        {

            break;
        }
}



Range currentCells = ExcelAddIn3.Globals.ThisAddIn.Application.get_Range(firstCol, lastCol);
currentCells.Select();

        }
    }
}

现在我目前的解决方案是

Range currentCells = ExcelAddIn3.Globals.ThisAddIn.Application.get_Range(firstCol, lastCol);
currentCells.Select();

然而,这使用地址而不是 vba 代码的序数符号。

4

1 回答 1

0

在这里找到了更好的解决方案 http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/afd7f2ab-ee1e-495f-8071-9f2377a603fb

    private void FindUsedRange()
{
   Excel.Worksheet wks = this.ActiveSheet as Excel.Worksheet;

   if (wks != null)
   {
      wks.UsedRange.Select();
   }
}

这与我使用 VSTO 编写的代码相同。希望它可以帮助别人。

于 2013-03-07T20:27:57.377 回答