Consulting

Results 1 to 9 of 9

Thread: Search that loops through inputs

  1. #1

    Search that loops through inputs

    Hello-
    I need a search/find structure. I have an import sheet that has numerical values for certain samples. I also have a 'results' sheet. These are in different workbooks. I would like the search to take the samples names from the 'results' page (which are in a single column) and search for the corresponding numerical values from the import sheet. I have some code for the search but cannot get the loop to work. This code is pretty dirty since I am relatively new to vb so I'm sure it should be improved. I have included the base code.

    Thanks in advance,
    Mark


    Dim qsearch As String
        Dim Cel As Range
        Dim temp2 As Variant
        Dim temp1 As Variant
        Dim FirstAddress As String
        Dim n As Integer
    For n = 69 To 110
                With Range("B:B")
                    Windows("CForm.xls").Activate
                    qsearch = Sheets("Q").Range("L" & n).Value
                   Set Cel = .Find(What:=qsearch, LookIn:=xlValues, _
                   LookAt:=xlWhole, MatchCase:=False)
                        If Not Cel Is Nothing Then
                             FirstAddress = Cel.Address
                         Do
                              temp1 = Cel.Address
                              Windows(wbtemp).Activate   'import sheet
                              Range(temp1).Activate
                              ActiveCell.Offset(rowOffset:=1, columnOffset:=1).Activate
                              temp2 = ActiveCell.Value
                              Windows("CForm.xls").Activate
                              Sheets("Q").Select
                              Range("M" & n).Value = temp2
                            Set Cel = .FindNext(Cel)
                        Loop While Not Cel Is Nothing And Cel.Address <> FirstAddress
                  End If
                 End With
            Next n

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Try moving the
    While Not Cel Is Nothing And Cel.Address <> FirstAddress
    to the Do statement
    so it is like this
    Do While Not Cel Is Nothing And Cel.Address <> FirstAddress
    vb code 
    Loop

  3. #3
    It doesn't seem to like that. It says End if block without if...

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by msmith
    It doesn't seem to like that. It says End if block without if...
    Rgius is what is suggested

    Dim qsearch As String
    Dim Cel As Range
    Dim temp2 As Variant
    Dim temp1 As Variant
    Dim FirstAddress As String
    Dim n As Integer
    For n = 69 To 110
    With Range("B:B")
    Windows("CForm.xls").Activate
    qsearch = Sheets("Q").Range("L" & n).Value
    Set Cel = .Find(What:=qsearch, LookIn:=xlValues, _
    LookAt:=xlWhole, MatchCase:=False)
    If Not Cel Is Nothing Then
    FirstAddress = Cel.Address
    Do While Not Cel Is Nothing And Cel.Address <> FirstAddress
    temp1 = Cel.Address
    Windows(wbtemp).Activate 'import sheet
    Range(temp1).Activate
    ActiveCell.Offset(rowOffset:=1, columnOffset:=1).Activate
    temp2 = ActiveCell.Value
    Windows("CForm.xls").Activate
    Sheets("Q").Select
    Range("M" & n).Value = temp2
    Set Cel = .FindNext(Cel)
    Loop
    End If
    End With
    Next n
    Cam't test properly as you don't set all the variables in this code
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Range (B:B) is being reset to a range on CForm.xls Set your range first, such as

    WBTemp = "WBT.xls"
    Set SRange = Workbooks(WBTemp).Worksheets("sheet1").Range("B:B")
    For n = 69 To 110
    With SRange
    This should work with your original code.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Thanks. It works great now.


    Quote Originally Posted by mdmackillop
    Range (B:B) is being reset to a range on CForm.xls Set your range first, such as
    [VBA]
    WBTemp = "WBT.xls"
    Set SRange = Workbooks(WBTemp).Worksheets("sheet1").Range("B:B")

    For n = 69 To 110
    With SRange
    [/VBA]
    This should work with your original code.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad to be of help.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Mark,
    For a "cleaned up" version, have a look at the following. It avoids the sheet activations etc. and can be run from when either workbook is active.

    Option Explicit
    Sub test()
    Dim qsearch As String
    Dim Cel As Range
    Dim FirstAddress As String
    Dim n As Integer
    Dim QSheet As Worksheet, SSheet As Worksheet
    Dim SRange As Range
    Set SSheet = Workbooks("WBT.xls").Sheets("Sheet1")
    Set SRange = SSheet.Range("B:B")
    Set QSheet = Workbooks("CForm.xls").Sheets("Q")
    For n = 69 To 110
    With SRange
    qsearch = QSheet.Range("L" & n).Value
    Set Cel = .Find(What:=qsearch, LookIn:=xlValues, _
    LookAt:=xlWhole, MatchCase:=False)
    If Not Cel Is Nothing Then
    FirstAddress = Cel.Address
    Do
    QSheet.Range("M" & n).Value = SSheet.Range(Cel.Address).Offset(1, 1)
    Set Cel = .FindNext(Cel)
    Loop While Not Cel Is Nothing And Cel.Address <> FirstAddress
    End If
    End With
    Next n
    End
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Thanks again! That would speed up code later when the number of results increase on the import sheet.

    Mark

Posting Permissions

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