How to cancel VBA code execution if cell ISEMPTY

Henceman

New Member
Joined
Oct 9, 2017
Messages
46
Im having a little trouble finding the correct syntax to stop macro, if certain cell is left unfilled.

The code is following, which extracts certain bit of info from the excel sheet and saves to txt file.

Private Sub Interactive Button to press()


Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
Set WorkRng = Application.Selection
Set WorkRng = ActiveSheet.Range("A1:E5") 'the range of cells which will be saved

Application.ScreenUpdating = False 'screen wont flicker when saving
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
If IsEmpty(Range("A1").Value) = True Then
MsgBox "Reference number is not filled!", vbRetryCancel
Exit Sub
End If

saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False

wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = False
Application.ScreenUpdating = True

End Sub



I have highlighted the part I need help with in red, right now, If the condition is True and the A1 cell is empty, then excel opens the selection in new window and does not save the file, But I want it to stop the code execution alltogether, so the user can correct the error (e.g fill the cell which was blank) and then execute the code again and then the save prompt appears.

So- if cell is empty, excel shows error, that code cannot continue until the number is filled, user fixes the error and this time code runs and displays the save promot...

I hope I managed to make myself understandable :)
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try moving the lines in red to just after this line
Code:
Set WorkRng = ActiveSheet.Range("A1:E5") 'the range of cells which will be saved
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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