Results 1 to 9 of 9

Thread: [RESOLVED] Newbie question

  1. #1

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

    Resolved [RESOLVED] Newbie question

    Quite simply - why wont this work?
    I'm not getting any errors. I've checked and it's definitely opening/referencing the correct excel file. I've kept it this simple just to see if I can get the basics working... (I can't )

    The console is writing out the Not Found message. It's definitely there! I even copied and pasted a load of other 1's around just in case.

    Why, oh why???
    VB Code:
    1. Imports Excel
    2. Imports Microsoft.Office.Core
    3.  
    4. Private moApp As Excel.Application
    5.  
    6.         moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    7.         moApp.Visible = True
    8.         Dim myFileName As String = "D:\My Documents\helpMe2.xls"
    9.         Dim oWB As Excel.Workbook = moApp.Workbooks.Open(myFileName)
    10.         Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Time"), Excel.Worksheet)
    11.  
    12.         Console.Write("Got here")
    13.         If oSht.Cells(3, 1) Is "1" Then 'found, take items from this row and next row
    14.             Console.Write("Found Number")
    15.             oSht.Cells(5, 6) = "Found"
    16.         Else
    17.             Console.Write("Nothing Found")
    18.         End If
    19.  
    20.         oWB.Close(True, myFileName)
    21.         oSht = Nothing
    22.         oWB = Nothing

    I also tried

    Cells.Items

    .ToString = "1"

    Nothing works. Please put me out of my misery.
    Stim

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

  2. #2
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: Newbie question

    why not just use 1 as an integer instead of a string ? If autocalculation is on "1" will be converted to the integer 1

  3. #3

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

    Re: Newbie question

    It doesn't matter if it's a String or an Integer.

    If I put text in there, which is what will actually be in the cells, it still doesn't work.

    Something I've done is just deeply flawed I think. Just don't know what.
    Stim

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

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Newbie question

    Try this
    VB Code:
    1. If oSht.Cells(3, 1).Value = 1
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

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

    Re: Newbie question

    The intellisense isn't allowing me the .Value option after the cell reference. Only .ToString and a few other things. I can do osht.Cells.Value

    I put this code in to try and check for things. It writes to the file without a problem, saves it, closes it. No errors at all. It just wont find the damn word that has to be in the cell in this example because it has just written it itself!!
    Stil writes the didn't find message. What is going on??? I've tried every combination I can think of.


    VB Code:
    1. Console.WriteLine("Got here")
    2.  
    3.         For i As Integer = 1 To 40
    4.             oSht.Cells(i, 1) = "FIND ME"  'Write Phrase in Cells
    5.         Next
    6.  
    7.         If oSht.Cells(10, 1).ToString = "FIND ME" Then  'Example: selected a cell from populated range to check
    8.             Console.WriteLine("Found it")
    9.         Else : Console.WriteLine("Didn't Find it")
    10.         End If
    Stim

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

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Newbie question

    OK, try this - you will need to Dim rngCheckCell as an excel range.
    VB Code:
    1. Set rngCheckCell = oSht.Cells(3, 1)
    2.        
    3.         If rngCheckCell.Value = 1 Then
    4.             Console.Write ("Found Number")
    5.             Set rngCheckCell = oSht.Cells(5, 6)
    6.             rngCheckCell.Value = "Found"
    7.         Else
    8.             Console.Write ("Nothing Found")
    9.         End If
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

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

    Re: Newbie question

    Nope. Still no joy. Thank you for the input though.

    I'm going for a ciggy before I put the computer through the window.
    I declared it as follows. Hope it's right. Because I've got Option Strict and Explicit On I also had to also cast it like so...


    Dim rngCheckCell As Excel.Range
    rngCheckCell = CType(oSht.Cells(3, 1), Range)

    etc....
    Stim

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

  8. #8

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

    Re: Find Cell Value

    I tried something different to see what would happen. After filling in cells with a string value I then assigned one of the cell's values to a String variable. The outputted it into a clear cell to see what it would say:

    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

    Here is what was in the Excel cell as the value:
    System.__ComObject


    What is that about??
    Last edited by stimbo; Oct 27th, 2006 at 03:44 AM.
    Stim

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

  9. #9

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

    Re: Newbie question

    Hi Declan,
    I finally got it working. It was the strange COM value being written to the excel file allowed me to narrow down that it was a casting issue in particular and hence the excel range was probably the most likely answer.

    The answer was a mix of both your answers. I finally got it working. Maybe this is exactly what you meant in the first place. If so, my apologies for not picking up on it. So 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
    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