For Next loop - introducing a variable to control my macro

tbablue

Active Member
Joined
Apr 29, 2007
Messages
482
Office Version
  1. 365
Platform
  1. Windows
The rudimentary code below passes a value to the macro PRINTz_ONE, runs PRINTz_ONE then introduces the next value, .......... and so on.
Sub PRINTz_GRP()
On Error Resume Next
With Range("SITEx_PICKER")
.Value = "ROLLED-UP": PRINTz_ONE
.Value = "NYC": PRINTz_ONE
.Value = "MUNICH": PRINTz_ONE
.Value = "WA": PRINTz_ONE
End With
End Sub

This works fine - but it isn't very flexible. I need to specify each site in the code when I want that value passed to PRINTz_ONE - and remove it when I don't.

I need a loop that will reference a lookup table within the workbook ('PPS'!D3:E12), where D contains a list of sites (NYC, PARIS, LONDON, etc) and column E contains the values YES or NO. I need the loop to only run PRINTz_ONE for the sites that have the value YES ascribed.

Can any one help me? Any assistance welcomed.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Maybe something like:

Code:
Sub Loop_Print()
Dim ws As Worksheet
Dim rng As Range
Dim r As Integer
Set ws = Sheets("PPS")
Set rng = ws.Range("D3")
r = 0
Do While rng.Offset(r, 0) <> ""
    If UCase(rng.Offset(r, 1)) = "YES" Then
        Printz_One (rng.Offset(r, 0))
    end if
    r = r + 1
Loop
End Sub

Sub Printz_One(SiteName As String)
    'do your printing stuff
End Sub
 
Upvote 0
One option would be something like this
Code:
Sub printz_grp()

   Dim Cl As Range
   
   For Each Cl In Sheets("PPS").Range("E3:E12")
      If LCase(Cl.Value) = "yes" Then Call printz_one(Cl.Offset(, -1).Value)
   Next Cl
End Sub

Sub printz_one(MySite As String)

End Sub
Where in the printz_one macro you replace using the range SITEx_PICKER with MySite
 
Upvote 0
Thank you all.

Notably PatOBrien198 & Fluff.

I was able to amend my code to the following:

Sub PRINTz_GRP()

On Error Resume Next

For Each Cl In Sheets("PPS").Range("E3:E12")
If LCase(Cl.Value) = "yes" Then Range("SITEx_PICKER").Value = (Cl.Offset(, -1).Value): PRINTz_ONE
Next Cl

End Sub

This now runs exactly as I need.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,189
Messages
6,129,418
Members
449,509
Latest member
ajbooisen

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