使用 VB.NET 使用 VBA 代码将特定单元格的值写入工作簿
Write value to specific cell to Workbook with VBA code using VB.NET
我在 Excel VBA 中有代码在第 2 列中找到特定值并将该值写入第 3 列中找到的行:
ThisWorkbook.Worksheets("Sheet1").Cells(Cells.Find("ProjectNumber", lookat:=xlWhole).Row, 3).Value = "TEST"
我正在尝试从我的 Visual Studio 应用程序中获取 运行 的功能。
Imports Excel = Microsoft.Office.Interop.Excel
Module Automate_Excel
Public xlApp As Excel.Application = Nothing
Public xlWorkBook As Excel.Workbook = Nothing
Public xlWorkSheet As Excel.Worksheet = Nothing
Sub Excel_FinishProject()
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("G:0 Databases\Projects Schedule.xlsx")
xlApp.Visible = True
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
'Write
xlWorksheet("Sheet1").Cells(Cells.Find("ProjectNumber", lookat:=xlWhole).Row, 3).Value = "TEST"
xlWorkBook.Close()
xlApp.Quit()
End Sub
End Module
它给我的错误如
xlwhole is not declared
和
cells is not declared
我的理解应该是来自Excel的类型库和代码如:
xlWorkSheet.Cells(2, 5) = "TEST"
确实使用了“细胞”。
您需要完全限定每个枚举。在这种情况下,
Excel.XlLookAt.xlWhole
在Excel/VBA环境中,它们只是一个基本的枚举。
Cells
在您的代码中也不完全合格。 Cells.Find
需要工作表限定符。 VB.NET 不知道没有限定符的 Cells
是什么。同样,在 VBA 环境中,您不必如此明确,但在 VB.NET 中您需要这样做,因为没有“默认上下文”
您的 xlWorkSheet
变量未编入索引。它已经包含对 xlWorkBook.Worksheets("Sheet1")
的单个引用 - 因此您无需再次指定它的名称。
此外,您应该在使用 Find
变量之前将其存储在 Range
变量中,而不是试图在一行中完成所有操作。然后你可以在尝试使用结果之前检查它是否“没有找到任何东西”,甚至可以在采取行动之前看到结果是什么
xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
Dim result as Excel.Range = xlWorkSheet.Cells.Find("ProjectNumber", lookat:=Excel.XlLookAt.xlWhole)
If result IsNot Nothing Then
' xlWorkSheet.Cells(result.Row, 3).Value = "TEST"
' OP says this works instead
xlWorkSheet.Cells(result.Row, 3) = "TEST"
End IF
我在 Excel VBA 中有代码在第 2 列中找到特定值并将该值写入第 3 列中找到的行:
ThisWorkbook.Worksheets("Sheet1").Cells(Cells.Find("ProjectNumber", lookat:=xlWhole).Row, 3).Value = "TEST"
我正在尝试从我的 Visual Studio 应用程序中获取 运行 的功能。
Imports Excel = Microsoft.Office.Interop.Excel
Module Automate_Excel
Public xlApp As Excel.Application = Nothing
Public xlWorkBook As Excel.Workbook = Nothing
Public xlWorkSheet As Excel.Worksheet = Nothing
Sub Excel_FinishProject()
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("G:0 Databases\Projects Schedule.xlsx")
xlApp.Visible = True
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
'Write
xlWorksheet("Sheet1").Cells(Cells.Find("ProjectNumber", lookat:=xlWhole).Row, 3).Value = "TEST"
xlWorkBook.Close()
xlApp.Quit()
End Sub
End Module
它给我的错误如
xlwhole is not declared
和
cells is not declared
我的理解应该是来自Excel的类型库和代码如:
xlWorkSheet.Cells(2, 5) = "TEST"
确实使用了“细胞”。
您需要完全限定每个枚举。在这种情况下,
Excel.XlLookAt.xlWhole
在Excel/VBA环境中,它们只是一个基本的枚举。
Cells
在您的代码中也不完全合格。 Cells.Find
需要工作表限定符。 VB.NET 不知道没有限定符的 Cells
是什么。同样,在 VBA 环境中,您不必如此明确,但在 VB.NET 中您需要这样做,因为没有“默认上下文”
您的 xlWorkSheet
变量未编入索引。它已经包含对 xlWorkBook.Worksheets("Sheet1")
的单个引用 - 因此您无需再次指定它的名称。
此外,您应该在使用 Find
变量之前将其存储在 Range
变量中,而不是试图在一行中完成所有操作。然后你可以在尝试使用结果之前检查它是否“没有找到任何东西”,甚至可以在采取行动之前看到结果是什么
xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
Dim result as Excel.Range = xlWorkSheet.Cells.Find("ProjectNumber", lookat:=Excel.XlLookAt.xlWhole)
If result IsNot Nothing Then
' xlWorkSheet.Cells(result.Row, 3).Value = "TEST"
' OP says this works instead
xlWorkSheet.Cells(result.Row, 3) = "TEST"
End IF