For loop in vba question?

tahir9

New Member
Joined
Aug 31, 2014
Messages
45
In my code I have three loops however the final two loops I want to be transitioning at the same time, so instead of next i or for it to work properly it should be next i and next z but it seems I can't do that is there a way around that... or a simpler solution that's not hitting me yet? Here is my code:
Code:
Sub dateofvisit()
Dim lastrow As Long, rng As Range, revenge As Range
Application.ScreenUpdating = False
Application.Calculation = xlManual
sheetlist = Array("Jackson", "Michael", "Bieber", "Nicole", "Reina", "TimC")
strsearch = Array("Jackson", "Michael", "Bieber", "Nicole", "Reina", "TimC")
Set rng = Range("B5:S2641")
Set revenge = Range("A4:Z4")

For Z = LBound(strsearch) To UBound(strsearch)
For i = LBound(sheetlist) To UBound(sheetlist)
        Sheets("Sheet1").Activate
        lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
        raio = "Revsev"
        
        For x = lastrow To 5 Step -1
                If ActiveSheet.Cells(x, 2) = strsearch(Z) _
                    And ActiveSheet.Cells(x, 11) <= 0.555 Then
                        Rows(x).Copy Sheets(sheetlist(i)).Cells(Rows.Count, 1).End(xlUp)(2)
                    End If
                Next x
            Next i
        Next Z
            
        
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
so essentially it would be start out as for z = blah to blah and i= blah to blah and it would end with next i and z I don't know if that's possible or if theres another way around this. Thanks.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
First I'd like to mention a possible error in your code.

lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

I don't think you can use Letter references in the Cells method. You have to modify this to...

lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlup).Row
 
Last edited:
Upvote 0
As for the rest of your code. I'm having trouble understanding it. It's always simpler if you just post a dataset of what it looks like before you run the macro, and a seperate dataset of what it should look like after you run the macro.
 
Upvote 0
First I'd like to mention a possible error in your code.

lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

I don't think you can use Letter references in the Cells method.
The column argument for the Cells property can take either the column number or the column's letter designation. I would note the ActiveSheet reference could be omitted as it is the default when omitted (of course including it does no harm), but aside from that, the code line is fine as written.
 
Upvote 0
WarPiglet, the column letter designation "A" is definitely a valid argument for Cells().

tahir9, I am also having trouble understanding your code. From what I understand though, there is a possibility to use a "helper" variable. So you would eliminate one of the for loops, and use a helper variable instead. Assign the helper the first value it is needed and, if can be incremented as it seems, you can assign it's new value at the end of your for loop, satisfying both variables requirements. Hope this helped.
 
Upvote 0
The column argument for the Cells property can take either the column number or the column's letter designation. I would note the ActiveSheet reference could be omitted as it is the default when omitted (of course including it does no harm), but aside from that, the code line is fine as written.
Awesome. I didn't know that.
 
Upvote 0
Sorry guys for poor format still a beginner and trying to get better at writing the code. The code in simpler version is saying look for this name in column in 2 then go look at column 7 and see if its less than .555, if its true copy and paste the value to the next sheet, then go the next row and do the same kind of thing. Once it does that for one name, then its supposed to move onto the next name in the array i.e Michael.

So pdevito you're saying instead of having the for loop for the names of the people just have the value z = z+1 after the next x.

And would I label z the same thing it is now instead of without the for in front?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top