Find two values, do something, then findnext instance of both, do some more things

ajm

Well-known Member
folks, i am working through a reformatting problem in another thread and came to a new problem along the way.

Find and Findnext:

in my active range, i set two variables to find.

Code:
Set c = .Find("DEBT2.P33", LookIn:=xlValues)    Set d = .Find("Previous Months Adjustments:", LookIn:=xlValues)
from the positions in which these strings are found, i offset to gather certain values. These values are then pasted to a new sheet.

the code then resets (findsnext) the variables as below:
Code:
Set c = .FindNext(c)            Set d = .FindNext(d)
the theory being that the code then finds the next instance of the c variable and the next instance of the d variable, offsetting from those addresses to gather the next lot of values. this continues until the loop has gathered data from every page.

However, on resetting c, which should then find the next instance of "DEBT2.P33" in my active sheet, the variable becomes "Previous Months Adjustments:", which is the d variable.

Can you have more than one Find/FindNext pair in a macro?
 

DanteAmor

Well-known Member
This way it is not possible:

Code:
Sub test2()
  Dim c As Range, r As Range, d As Range
  Set r = Sheets("sheet1").Range("A:A")
  Set c = r.Find("DEBT2.P33", , xlValues, xlWhole)
  Set d = r.Find("Previous Months Adjustments:", , xlValues, xlWhole)
  Set c = r.FindNext(c)
  Set d = r.FindNext(d)
  MsgBox c.Row
  MsgBox d.Row
End Sub
-------------------
But in this way yes:

Code:
Sub test()
  Dim c As Range, r As Range, d As Range
  Set r = Sheets("sheet1").Range("A:A")
  Set c = r.Find("DEBT2.P33", , xlValues, xlWhole)
  Set c = r.FindNext(c)
  MsgBox c.Row
  Set d = r.Find("Previous Months Adjustments:", , xlValues, xlWhole)
  Set d = r.FindNext(d)
  MsgBox d.Row
End Sub
-------------------

What do you need exactly?
 
  • Like
Reactions: ajm

ajm

Well-known Member
so do one find/find next, get my values from the adjacent cells, and then do a second find/find next and get the second lot of values from the adjacent cells?
 

DanteAmor

Well-known Member
so do one find/find next, get my values from the adjacent cells, and then do a second find/find next and get the second lot of values from the adjacent cells?
Take a test and check if it is what you need.
Otherwise, explain with examples what you need to look for and what you need to get.
 

ajm

Well-known Member
Code:
Sub test()


    Dim CurrMth As String, Class As String, Beds As String, CMAmtRsd As String, PMBedDays As String, PMAmtRsd As String


    Dim Rng As Range, firstAddress as Range
    Dim c As Range, d As Range
    Dim pasteCell As Range
    Dim DestSht As Worksheet




   
    Set DestSht = Worksheets("AmtsRaised") '///will paste values to this sheet
        
    CurrMth = Trim(Right(ActiveSheet.Range("c2"), 6)) '///current calendar month
   
    Set Rng = ActiveSheet.Range("a1", Range("a1048576").End(xlUp)) '///where is the data that has been imported from ERP
 
        Set c = Rng.Find("DEBT2.P33", LookIn:=xlValues) '/// what do we look for first
               
        If Not c Is Nothing Then
            firstAddress = c.Address '///  if value exists, assign variable name to its address (location on page)
        
            Do '///what actions do we want to perform

'///assign variable names to desired values. these are offset from the Find value
                CMClass = Trim(Right(c.Offset(2, 0).Value, Len(c.Offset(2, 0).Value) - InStr(c.Offset(2, 0).Value, ":")))
                Beds = Trim(Right(c.Offset(8, 0).Value, Len(c.Offset(8, 0).Value) - InStr(c.Offset(8, 0).Value, ":")))
                CMAmtRsd = Trim(c.Offset(10, 7).Value)
        
                
                With DestSht
                                
                    Set pasteCell = .Range("b1048576").End(xlUp).Offset(1, 0) '///find the first empty cell in Col b
                
                    '/// paste the variables adjacent to first available cell in Col B
                    pasteCell.Offset(0, -1).Value = "Current Month"
                    pasteCell.Value = CurrMth
                    pasteCell.Offset(0, 1).Value = CMBeds
                    pasteCell.Offset(0, 2).Value = CMClass
                    pasteCell.Offset(0, 3).Value = CMAmtRsd


                End With
                      
 '///find next instance of what we are looking for
                Set c = Rng.FindNext(c)


            '///loop through this process until there are no more values to find
            Loop While Not c Is Nothing And c.Address <> firstAddress


        End If

'///now look for second value we need
            Set d = Rng.Find("Previous Months Adjustments:", LookIn:=xlValues)
        
            If Not d Is Nothing Then
                firstAddress = d.Address  '///  if value exists, assign variable name to its address (location on page)
        
                Do   '///find the values we want relative to d 
                    PMClass = Trim(Right(d.Offset(2, 0).Value, Len(d.Offset(2, 0).Value) - InStr(d.Offset(2, 0).Value, ":")))
                    PMBedDays = Trim(Right(d.Offset(3, 0).Value, Len(d.Offset(3, 0).Value) - InStr(d.Offset(3, 0).Value, ":")))
                    PMAmtRsd = Trim(d.Offset(5, 7).Value)
             
                    '
                    '///paste these variables to the bottom of the list on the DestSht
     
                    With DestSht
                        Set pasteCell = .Range("b1048576").End(xlUp).Offset(1, 0)
                      
                        'Copy and paste
                        pasteCell.Offset(0, -1).Value = "Previous Months Adjustments:"
                        pasteCell.Value = CurrMth
                        pasteCell.Offset(0, 1).Value = PMBedDays
                        pasteCell.Offset(0, 2).Value = PMClass
                        pasteCell.Offset(0, 3).Value = PMAmtRsd
     
                    End With
                    
        
                    Set d = Rng.FindNext(d) '///next instance
            
                Loop While Not d Is Nothing And d.Address <> firstAddress
            End If
        
    End Sub
Dante, this is my code in full. it loops through a data extract which has been imported from our ERP and when it finds the search values, it returns adjacent values. i originally posted the query here: https://www.mrexcel.com/forum/excel-questions/1108104-reformatting-data-extract.html.
 
Last edited:

Akuini

Well-known Member
I don't quite understand what you want but maybe this example could help:
The code search for X & Y.
You can see the loop give you the address of X & Y found incrementally (see debug result).

Code:
[FONT=lucida console][color=Royalblue]Sub[/color] doubleFind2()
[i][color=seagreen]'https://www.mrexcel.com/forum/excel-questions/1108234-find-two-values-do-something-then-findnext-instance-both-do-some-more-things.html[/color][/i]

[color=Royalblue]Dim[/color] c [color=Royalblue]As[/color] Range, d [color=Royalblue]As[/color] Range, f [color=Royalblue]As[/color] Range, z [color=Royalblue]As[/color] Range
[color=Royalblue]Dim[/color] i [color=Royalblue]As[/color] [color=Royalblue]Long[/color]

Application.ScreenUpdating = [color=Royalblue]False[/color]
    [color=Royalblue]Set[/color] f = Range([color=brown]"A1"[/color], Cells(Rows.count, [color=brown]"A"[/color]).[color=Royalblue]End[/color](xlUp))
    [color=Royalblue]Set[/color] z = Range([color=brown]"A:A"[/color]).Find([color=brown]"X"[/color], LookIn:=xlValues, lookAt:=xlPart, Searchorder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=[color=Royalblue]False[/color], SearchFormat:=[color=Royalblue]False[/color])
    [color=Royalblue]If[/color] z [color=Royalblue]Is[/color] [color=Royalblue]Nothing[/color] [color=Royalblue]Then[/color] [color=Royalblue]Exit[/color] [color=Royalblue]Sub[/color]
    i = z.Row

    [color=Royalblue]Do[/color]
        [color=Royalblue]Set[/color] c = f.Find(What:=[color=brown]"X"[/color], LookIn:=xlValues, After:=Cells(i, [color=brown]"A"[/color]), SearchDirection:=xlNext)
        [color=Royalblue]Set[/color] d = f.Find(What:=[color=brown]"Y"[/color], LookIn:=xlValues, After:=Cells(c.Row, [color=brown]"A"[/color]))
                
        [color=Royalblue]If[/color] d [color=Royalblue]Is[/color] [color=Royalblue]Nothing[/color] [color=Royalblue]Then[/color] [color=Royalblue]Exit[/color] [color=Royalblue]Do[/color]
        [color=Royalblue]If[/color] d.Row < c.Row [color=Royalblue]Then[/color] [color=Royalblue]Exit[/color] [color=Royalblue]Do[/color]
            [i][color=seagreen]'do something here[/color][/i]
            Debug.Print c.Address & [color=brown]" : "[/color] & d.Address
                
        i = d.Row
    
    [color=Royalblue]Loop[/color] [color=Royalblue]Until[/color] z.Row = c.Row

Application.ScreenUpdating = [color=Royalblue]True[/color]

[color=Royalblue]End[/color] [color=Royalblue]Sub[/color][/FONT]

Example:
Excel 2013 32 bit
A
1
1​
2
X
3
Y
4
4​
5
X
6
6​
7
7​
8
8​
9
9​
10
10​
11
Y
12
12​
13
13​
14
X
15
15​
16
16​
17
17​
18
Y
19
19​
20
20​
Sheet: Sheet2

DEBUG RESULT:

$A$2 : $A$3
$A$5 : $A$11
$A$14 : $A$18
 

ajm

Well-known Member
I don't quite understand what you want but maybe this example could help:
The code search for X & Y.
You can see the loop give you the address of X & Y found incrementally (see debug result).

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] doubleFind2()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1108234-find-two-values-do-something-then-findnext-instance-both-do-some-more-things.html[/COLOR][/I]

[COLOR=Royalblue]Dim[/COLOR] c [COLOR=Royalblue]As[/COLOR] Range, d [COLOR=Royalblue]As[/COLOR] Range, f [COLOR=Royalblue]As[/COLOR] Range, z [COLOR=Royalblue]As[/COLOR] Range
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]

Application.ScreenUpdating = [COLOR=Royalblue]False[/COLOR]
    [COLOR=Royalblue]Set[/COLOR] f = Range([COLOR=brown]"A1"[/COLOR], Cells(Rows.count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp))
    [COLOR=Royalblue]Set[/COLOR] z = Range([COLOR=brown]"A:A"[/COLOR]).Find([COLOR=brown]"X"[/COLOR], LookIn:=xlValues, lookAt:=xlPart, Searchorder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=[COLOR=Royalblue]False[/COLOR], SearchFormat:=[COLOR=Royalblue]False[/COLOR])
    [COLOR=Royalblue]If[/COLOR] z [COLOR=Royalblue]Is[/COLOR] [COLOR=Royalblue]Nothing[/COLOR] [COLOR=Royalblue]Then[/COLOR] [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
    i = z.Row

    [COLOR=Royalblue]Do[/COLOR]
        [COLOR=Royalblue]Set[/COLOR] c = f.Find(What:=[COLOR=brown]"X"[/COLOR], LookIn:=xlValues, After:=Cells(i, [COLOR=brown]"A"[/COLOR]), SearchDirection:=xlNext)
        [COLOR=Royalblue]Set[/COLOR] d = f.Find(What:=[COLOR=brown]"Y"[/COLOR], LookIn:=xlValues, After:=Cells(c.Row, [COLOR=brown]"A"[/COLOR]))
                
        [COLOR=Royalblue]If[/COLOR] d [COLOR=Royalblue]Is[/COLOR] [COLOR=Royalblue]Nothing[/COLOR] [COLOR=Royalblue]Then[/COLOR] [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Do[/COLOR]
        [COLOR=Royalblue]If[/COLOR] d.Row < c.Row [COLOR=Royalblue]Then[/COLOR] [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Do[/COLOR]
            [I][COLOR=seagreen]'do something here[/COLOR][/I]
            Debug.Print c.Address & [COLOR=brown]" : "[/COLOR] & d.Address
                
        i = d.Row
    
    [COLOR=Royalblue]Loop[/COLOR] [COLOR=Royalblue]Until[/COLOR] z.Row = c.Row

Application.ScreenUpdating = [COLOR=Royalblue]True[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]

Example:
Excel 2013 32 bit
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]A[/COLOR]​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]1[/COLOR]​
1​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]2[/COLOR]​
X
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]3[/COLOR]​
Y
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]4[/COLOR]​
4​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]5[/COLOR]​
X
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]6[/COLOR]​
6​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]7[/COLOR]​
7​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]8[/COLOR]​
8​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]9[/COLOR]​
9​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]10[/COLOR]​
10​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]11[/COLOR]​
Y
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]12[/COLOR]​
12​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]13[/COLOR]​
13​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]14[/COLOR]​
X
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]15[/COLOR]​
15​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]16[/COLOR]​
16​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]17[/COLOR]​
17​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]18[/COLOR]​
Y
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]19[/COLOR]​
19​
[COLOR=[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FFFFFF"]#FFFFFF[/URL] ]20[/COLOR]​
20​

<tbody>
</tbody>
Sheet: Sheet2

<tbody>
</tbody>

DEBUG RESULT:

$A$2 : $A$3
$A$5 : $A$11
$A$14 : $A$18
Akuini, thanks for your code. I actually got mine to work but yours looks a lot tidier so will play around with it when i have a bit of time up my sleeve.

Do you have problems with your code?
Dante, no problem with the code i posted.

I have actually added in a workbooks.open snippet at the start so that the original export file can be found, opened, and parsed. It runs a little slower now. would you be interested in seeing it to see if you can identify anything i have done inefficiently?

As to my posting the final code above, I like to post what worked for me in the end so that the thread has closure for anyone else looking for something similar. There are so many threads on the different forums where the OP asks their question, and someone responds, and then nothing.
 
Last edited:

Some videos you may like

This Week's Hot Topics

Top