Results 1 to 4 of 4

Thread: [RESOLVED] Get Contents of Cell in Excel Problem

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Resolved [RESOLVED] Get Contents of Cell in Excel Problem

    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:
    1. moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)  'Create Excel Object
    2.  
    3.         moApp.Visible = False                                   'Make it invisible
    4.  
    5.         Dim myFileName As String = "D:\My Documents\helpMe2.xls" 'This is the file to open
    6.         Dim oWB As Excel.Workbook = moApp.Workbooks.Open(myFileName)    'Open the workbook
    7.         Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Time"), Excel.Worksheet)  'Open Worksheet Name
    8.  
    9.         Console.WriteLine("Got here")
    10.  
    11.         For i As Integer = 1 To 40
    12.             oSht.Cells(i, 1) = "FIND ME"  'Write Phrase in Cells as a test - it works fine
    13.         Next
    14.  
    15.         If oSht.Cells(10, 1).ToString = "FIND ME" Then  'Example: select a cell from populated range to check
    16.             Console.WriteLine("Found it")
    17.         Else : Console.WriteLine("Didn't Find it")   'This is all I ever get
    18.         End If
    19.  
    20.         oWB.Save()
    21.         oWB.Close(True)
    22.         oSht = Nothing
    23.         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:
    1. Dim myValue As String = oSht.Cells(10, 1).ToString   'put value from cell into String
    2.  
    3. 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:
    1. If oSht.Cells.Item(10, 1).ToString = "FIND ME" Then   'etc
    2.  
    3.  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?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get Contents of Cell in Excel Problem

    I've not done much Office Automation so this is an intuitive answer and I can't guarantee that it is correct.

    This code:
    VB Code:
    1. If oSht.Cells(10, 1).ToString = "FIND ME" Then
    is getting an Excel cell, converting it to a string and then comparing that to "FIND ME". What you need to do is get the CONTENTS of the cell, cast that as String and then compare that to the target. You get the contents of a cell via its Value property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Get Contents of Cell in Excel Problem

    It was intuititive to me as well but didn't work. I have tried every combination I could think of (value, etc...).

    I think it's a casting problem of sorts. Found this link on MSDN about that strange message I got when i put the value into a string and outputted it again but can't really figure out if the answer is in front of me. Casting

    I tried casting an excel range without any joy and a few others (Strings etc... but it aint working).

    I really thought this was going to be the easy bit!
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: I got it working!!!!!!!

    It only took about 20 hours but hey.

    For anyone who may be interested:

    VB Code:
    1. 'Need to specify cell as an Excel Range - cast it
    2.   Dim myRange As Excel.Range = CType(oSht.Cells(10, 1), Range)  'Will replace cell values with array i to loop
    3.  
    4. 'Then to compare
    5.    If myRange.Value.ToString = "FIND ME" Then  'check from populated range to
    6.             Console.WriteLine("Found it")
    7.         Else : Console.WriteLine("Didn't Find it")   'This is all I get
    8.    End If

    EDIT: Or simply something like -
    VB Code:
    1. If Not CType(oSht.Cells(10, 1), Range).Value.ToString = "FIND ME" Then  
    2.             Console.WriteLine("Not Found")
    3.         Else : Console.WriteLine("Blimey, Found it")  
    4.   End If

    It has already become a better day.
    Last edited by stimbo; Oct 27th, 2006 at 05:08 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width