Try this:
Dim v As Object = CType(ExcelWS.Cells(1, 1), Range).Value2
Dim s As String = CStr(v)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Using VB.NET in Visual Studio 2022, I can write to an Excel cell but I can't read from it. Even after providing the row and column numbers, it still thinks it is returning a Range.
In the following code, writing to the cell works. You can see the new value if you put a watch on it.
Dim ExcelApp As New Excel.Application
Dim ExcelWBs As Excel.Workbooks
Dim ExcelWB As Excel.Workbook
Dim ExcelWS As Excel.Worksheet
ExcelWBs = ExcelApp.Workbooks
ExcelWB = ExcelWBs.Add
ExcelWS = ExcelWB.Sheets.Add
ExcelWS.Cells(1, 1) = "qwerty"
However, trying to read the cell throws an exception.
"ExcelWS.Cells(1, 1).Value2" gives the message "Public member 'Value2' on type 'Range' not found."
I get the same exception if I try ExcelWS.Cells(1, 1).Value2
in immediate mode. Furthermore, autocomplete doesn’t show a Value2
member (but the watch does). And debug.print(ExcelWS.Cells(1, 1)
gives:
Conversion from type 'Range' to type 'String' is not valid.
Try this:
Dim v As Object = CType(ExcelWS.Cells(1, 1), Range).Value2
Dim s As String = CStr(v)