loop through Rows in Range and check value in a column

Qroozn

Well-known Member
Joined
Mar 12, 2002
Messages
544
Hey guys. I haven't done VBA for years - but am trying to help out a colleague.

We currently have a sheet with approx. 50 pages. Different businesses want to see different pages printing.
Today.. someone goes through and manually works out which pages to print.


I have the below sample in Sheet1 Range A1:D5.
Lets assume I am running VBA for Business B.
I would like to
1) loop through each row in range and identify if Column "C" (businessB) has a value of "YES".
2) if the value is "YES" then I want to Set the TAB NAME (Column A) to print

Alternatively I might run the macro for BusinessA or C so am looking for appropriate column referencing.
Can someone please help out with VBA code? Thanks

Print Page BusinessA BusinessB BusinessC
Page 1 Yes Yes
page 2 Yes
page 3 Yes
page 4 Yes
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I was confused with "2) if the value is "YES" then I want to Set the TAB NAME (Column A) to print"

try this though:
Code:
Private Sub testZ()


Dim lsheet1 As Worksheet
Dim lrow1 As Long
Dim BizVal As Variant
Dim x As Long
Dim i As Long
Dim OsTR As String
Dim NsTR As String


Set lsheet1 = ActiveWorkbook.ActiveSheet


Application.ScreenUpdating = False


BizVal = "Business" & InputBox("Enter A, B or C")


Select Case BizVal
   Case "BusinessA"
      i = 2
   Case "BusinessB"
      i = 3
   Case "BusinessC"
      i = 4
End Select


lrow1 = lsheet1.Cells(Rows.Count, i).End(xlUp).Row


For x = 2 To lrow1
        If lsheet1.Cells(x, i) = "Yes" Then
        
        OsTR = lsheet1.Cells(x, 1)
        NsTR = Right(OsTR, Len(OsTR) - 5)
        With lsheet1
                ActiveWorkbook.Sheets("Sheet1").PrintOut From:=NsTR, To:=NsTR
        End With


        End If
Next x


Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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