VBA - I'm getting in a muddle.........

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,055
Office Version
  1. 365
Platform
  1. Windows
I've got some code that basically does this;

- changes the value of cell J1 using a data validation list, pulls through data using lookups etc
- if "print" or "print only" is in cell A5, it prints a copy of a payslip
- otherwise it emails the payslip
- loops through the list til the end

This is what I need it to do;

I want it to print the payslips if cell A5 says "print" or "print only" and also cell E5 contains "BACS" or "PAYE".

I want it to email the payslips if cell A5 doesn't say "print" or "print only" and also cell E5 contains "BACS" or "PAYE".

Otherwise it does nothing.

I think I'm 99% there but I cannot get this to work!

TIA

Code:
Sub LoopthroughDataValidation()
    Dim rCell As Range
    Dim Data As Range
    Dim a As Range
    Set a = Range("J1")
    
    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("A5") = "Print" Then
    Range("Payslip").PrintOut
    ElseIf Sheets("Pay Advice").Range("A5") = "Print only" Then
    Range("Payslip").PrintOut
    Else
    
  Dim FileName As String

    If ActiveWindow.SelectedSheets.Count > 1 Then
        MsgBox "There is more then one sheet selected," & vbNewLine & _
               "be aware that every selected sheet will be published"
    End If

    'Call the function with the correct arguments
    'Tip: You can also use Sheets("Sheet3") instead of ActiveSheet in the code(sheet not have to be active then)
     'FileName = RDB_Create_PDF(ActiveSheet, "", True, False)
     FileName = RDB_Create_PDF(ActiveSheet, "C:\Payslip.pdf", True, False)

    'For a fixed file name and overwrite it each time you run the macro use
    'RDB_Create_PDF(ActiveSheet, "C:\Users\Ron\Test\YourPdfFile.pdf", True, True)

    If FileName <> "" Then
        RDB_Mail_PDF_Outlook FileName, Sheets("Pay Advice").Range("A5"), "PRIVATE & CONFIDENTIAL", _
                             "Please find attached your Payslip for the work done in the previous month." _
                           & vbNewLine & vbNewLine & "Regards", False
    Else
        MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
               "Microsoft Add-in is not installed" & vbNewLine & _
               "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
               "The path to Save the file in arg 2 is not correct" & vbNewLine & _
               "You didn't want to overwrite the existing PDF if it exist"
    End If
    End If
       
    Next rCell
    
    Else
    End If
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,203,192
Messages
6,054,031
Members
444,696
Latest member
VASUCH

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