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
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