2013-03-05 59 views
0

你好傢伙即時嘗試將一些現有的代碼從VBA轉換到VSTO,到目前爲止我一直非常成功。 這是我想要轉換的代碼。轉換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 

然而,當我在.net中得到這部分

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

我不知道該如何處理?有沒有人有任何想法,並可以指出我在正確的方向。

這裏是我的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代碼的順序符號。

+0

在vsto中可以有很多更好的替代方法,不要從VBA複製每個單詞 – 2013-03-06 08:17:56

回答

相關問題