This has me stuck. I've tried everything I can and still no joy. Why wont the following work. I'm getting no error messages and the console is writing out the 'Not Found' message so it's doing all the code.
For this example I've stripped the code right down to the most basic. I write text into a column of excel cells with the phrase "FIND ME". I then want to say if the content of the cell (pick one from the populated range of cells) = "FIND ME" then Do stuff. It wont work. I've declared variables etc... here's the relevant code:
VB Code:
moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application) 'Create Excel Object moApp.Visible = False 'Make it invisible Dim myFileName As String = "D:\My Documents\helpMe2.xls" 'This is the file to open Dim oWB As Excel.Workbook = moApp.Workbooks.Open(myFileName) 'Open the workbook Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Time"), Excel.Worksheet) 'Open Worksheet Name Console.WriteLine("Got here") For i As Integer = 1 To 40 oSht.Cells(i, 1) = "FIND ME" 'Write Phrase in Cells as a test - it works fine Next If oSht.Cells(10, 1).ToString = "FIND ME" Then 'Example: select a cell from populated range to check Console.WriteLine("Found it") Else : Console.WriteLine("Didn't Find it") 'This is all I ever get End If oWB.Save() oWB.Close(True) oSht = Nothing oWB = Nothing
I tried assigning the value that was in one of the populated cells to a String variable and then output it:
VB Code:
Dim myValue As String = oSht.Cells(10, 1).ToString 'put value from cell into String oSht.Cells(2, 2) = myValue 'Now output it into different cell to see what it is
All I got was this in the cell: System.__ComObject
What does that mean???
I tried different formats such as these but to no avail:
VB Code:
If oSht.Cells.Item(10, 1).ToString = "FIND ME" Then 'etc If oSht.Cells(10, 1) Is "FIND ME" Then 'etc
I'm totally beaten and have gone past the angry stage to just curious as to what the answer is. What have I done wrong? Anyone got the answer please?




Reply With Quote