Results 1 to 3 of 3

Thread: [RESOLVED] Formated Excel Sheet, Unk No of Cells, Search

  1. #1

    Thread Starter
    Lively Member jkmcgrath's Avatar
    Join Date
    Dec 2004
    Posts
    79

    Resolved [RESOLVED] Formated Excel Sheet, Unk No of Cells, Search

    Hi,

    I need to be able to open an already filled in 2003 excel file (.xls) search a column for data and then return the row that matches the criteria.

    The sheet has formatting at the top and bottom and is dynamic, it is updated regularly so number of row is an unknown when opening the file. Here is some of the code that I have found by RobDog888 and some others. So I can connect and get specific information from a specified cell but I am lost on how to baiscly perform an SQL statement against it. Any help will be welcomed.
    VB Code:
    1. Option Explicit
    2.  
    3. 'Add a reference to ms excel xx.0 object library
    4.     Private Sub command1_click()
    5.     Dim oapp As Excel.Application
    6.     Dim oWB As Excel.Workbook
    7.     Set oapp = New Excel.Application
    8.     oapp.Visible = False
    9.     Set oWB = oapp.Workbooks.Open("M:\programming\VB6\reset\R50093.xls")
    10.     MsgBox oapp.Workbooks(1).Sheets("RS-04").Cells(7, 1) 'Cell A1
    11.    
    12.    
    13. Dim topCel As Range, bottomCel As Range
    14. Dim NoOfRows As Integer, a As Integer
    15.  
    16. 'Count entries
    17. With ActiveSheet
    18.     Set topCel = .Range("A7")
    19.     Set bottomCel = .Cells((65536), topCel.Column).End(xlUp)
    20.         If bottomCel.Row <= topCel.Row Then
    21.         Exit Sub
    22.         End If
    23. NoOfRows = .Range(topCel, bottomCel).Rows.Count
    24. MsgBox NoOfRows + 6
    25.  
    26. End With
    27.  
    28. oWB.Close
    29.     Set oWB = Nothing
    30.     oapp.Quit
    31.     Set oapp = Nothing
    32. End Sub
    Just an infant in VB years

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Formated Excel Sheet, Unk No of Cells, Search

    First off, don't use ActiveSheet - use the sheet name (or object variable) instead. You should also specify workbooks by name (or object variable) too. Both these issues can lead to your program crashing, or working with the wrong sheet/book. Corrections shown in the code below.

    To find a match you can use a loop, eg:
    VB Code:
    1. Set oWB = oapp.Workbooks.Open("M:\programming\VB6\reset\R50093.xls")
    2.     MsgBox [b]oWB.Sheets("RS-04")[/b].Cells(7, 1) 'Cell A1
    3.    
    4. Dim topCel As Range, bottomCel As Range
    5. Dim NoOfRows As Integer, a As Integer
    6.  
    7. With [b]oWB.Sheets("RS-04")[/b]
    8.     Set topCel = .Range("A7")
    9.     Set bottomCel = .Cells((65536), topCel.Column).End(xlUp)
    10.         If bottomCel.Row <= topCel.Row Then
    11.         Exit Sub
    12.         End If
    13. NoOfRows = .Range(topCel, bottomCel).Rows.Count
    14. MsgBox NoOfRows + 6
    15.  
    16.  
    17. [b]   'Find your value[/b]
    18. Dim iRow as Integer
    19. Dim iCol as Integer
    20.     iRow = 7
    21.     iCol = 1 'Column to search in
    22.     Do While .Cells(iRow, 1)  <> ""  'assumes there are values in column 1 for all rows to search
    23.       If .Cells(iRow, iCol) = "[i]your text[/i]" Then
    24.         Msgbox "Match found in row " & iRow & vbCr & "(do something else here)"
    25.       End If
    26.       iRow = iRow + 1
    27.     Loop
    28.  
    29.  
    30. End With

  3. #3

    Thread Starter
    Lively Member jkmcgrath's Avatar
    Join Date
    Dec 2004
    Posts
    79

    Re: Formated Excel Sheet, Unk No of Cells, Search

    Roger that Si thanks!!!!!!

    I will give that a shot.
    Just an infant in VB years

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