Looping Code - problem

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,055
Office Version
  1. 365
Platform
  1. Windows
I have this code which works fine;

Code:
Sub LoopthroughDataValidation()
    Dim rCell As Range
    Dim Data As Range
    Dim a As Range
    Set a = Range("A2")
    
    If MsgBox("Are you sure you want to print/email ALL payslips?", vbYesNo) = vbYes Then
    
    For Each rCell In Range("Performers")
    
    a.Value = rCell.Value
    
    'If Sheets("Pay Advice").Range("E2") = "print" Then
    Range("Payslip").PrintOut
    'ElseIf Sheets("Pay Advice").Range("E2") = "print only" Then
    'Range("Payslip").PrintOut
    'End If
    
    'EMAIL CODE
       
    Next rCell
    
    Else
    End If
End Sub

However if I change it so that the ' is removed from the 4 lines, it fails to print if cell E2 contains "Print".

Code:
Sub LoopthroughDataValidation()
    Dim rCell As Range
    Dim Data As Range
    Dim a As Range
    Set a = Range("A2")
    
    If MsgBox("Are you sure you want to print/email ALL payslips?", vbYesNo) = vbYes Then
    
    For Each rCell In Range("Performers")
    
    a.Value = rCell.Value
    
    If Sheets("Pay Advice").Range("E2") = "print" Then
    Range("Payslip").PrintOut
    ElseIf Sheets("Pay Advice").Range("E2") = "print only" Then
    Range("Payslip").PrintOut
    End If
    
    'EMAIL CODE
       
    Next rCell
    
    Else
    End If
End Sub

The contents of cell E2 is a vlookup that may or may not return "print" - is this the problem perhaps?

TIA
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
"print" isn't the same as "Print"

To make them the same you need to make them the same case. e.g
Code:
If lcase(Sheets("Pay Advice").Range("E2").text) = "print" Then
 
Upvote 0

Forum statistics

Threads
1,206,714
Messages
6,074,487
Members
446,072
Latest member
OrangeYellow

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