VBA Code to validate filled cells

slora00

New Member
Joined
Sep 8, 2022
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hello Community,

I've been trying to make a code and have not been able to crack it, so here I am asking for help.

The code should be pretty easy. The end goal is to save the Excel file in pdf. format in a specific folder, but before that, I need the macro to check if the following cells are NOT empty:
-D7, D9, D11, D18, D19
-E18, F18.
-J7, J9, J11, J18
-K19

Then, if any of them is Empty, I would like to show a Message Box saying something like "Please fill cell ...." and the cell that is empty. And also, if any of those cells is empty, then the macro should not save the file as PDF until all cells are filled.

Finally, when all cells are filled, save the file as PDF and show a Message Box saying "The file was saved succesfully"

Thank you in advance!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Change the folder path (in red) and the file name (in blue) to suit your needs.
Rich (BB code):
Sub ValidateCells()
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Range("D7, D9, D11, D18, D19,E18, F18,J7, J9, J11, J18,K19")
        If rng = "" Then
            MsgBox ("Please fill cell " & rng.Address(0, 0) & ".")
            rng.Select
            Exit Sub
        End If
    Next rng
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Test\FileName" & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,847
Members
449,051
Latest member
excelquestion515

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