Retrieve Excel Cell Value with VB Code
In VB, you can obtain the value of an Excel cell using the following code:
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim cellValue As String
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\YourExcelFile.xlsx")
Set xlSheet = xlBook.Sheets("Sheet1")
'获取单元格A1的值
cellValue = xlSheet.Range("A1").Value
MsgBox cellValue
xlBook.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
In this example, we begin by creating an Excel application object, opening a specified Excel file, and retrieving the value of a cell on a specified worksheet. Finally, we display the value of the cell in a message box. Before ending, it is important to release the Excel object in order to free up resources.