check empty cells before exporting

Beatrice

Board Regular
Joined
Sep 17, 2019
Messages
85
Office Version
  1. 2019
Platform
  1. Windows
currently I work on some code, and would like to improve its performance.
For example, before exporting the file, I want to run a check for some cells which is mandatory and must not be empty, otherwise pop up will show up.
I have made some trials,the code is fine but the exporting is still continue after the check.
I want the exporting to be done only if these specific cells is not empty.
It seems I miss some code in between the check and export, can anyone give me a hint?
Thanks in advance for helping.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Use exit sub, when it gets an empty cell, in your code.
 
Upvote 0
Use exit sub, when it gets an empty cell, in your code.

i have try but then it terminate from there, even the cell is with a value.
the situation is
1) To proceed a check before export, if blank/empty found, do not export.
2) if there is no input missing, then export must be proceed.

below I shorten my export code for understanding.
Thanks in advance for helping.

Rich (BB code):
'EXPORT BUTTON
Private Sub CommandButton2_Click()

   '--newly add--Test if mandatory is blank/empty
   If IsEmpty(Range("I4").Value) = True Then
      MsgBox "input missing"
   End If

Exit Sub

CreateObject("Wscript.Shell").popup "Please allow a few second for Exporting... ", 1, "Kind Reminder", vbSystemModal + 64
   
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet

Application.ScreenUpdating = False

' Open Source and Destination files

    ChDir " folder path"
    Workbooks.Open Filename:="workbook2.xlsx"
    
' Copy Dedicated Part
   
Workbooks("workbook1.xlsm").Sheets("sheet name").Range("range").Copy
Workbooks("workbook2.xlsx").Sheets("sheet name").Range("A1").PasteSpecial xlPasteValues

    Application.CutCopyMode = False
    Application.Goto Worksheets("sheet name").Range("A1"), True

' Save Destination file named as Today's date

    DD = Day(Date)
    MMM = Month(Date)
    YY = Year(Date)
    WBK = "workbook"
   
    Windows("workbook2").Activate
    ActiveWorkbook.SaveAs Filename:=WBK, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
   
    ActiveWindow.Close
   
    Call sourceSheet.Activate

MsgBox "done", vbOKOnly + vbInformation, "Export Completed"
   

End Sub
 
Upvote 0
It looks like you just need an else statement. Try the 2 changes in red

Rich (BB code):
'EXPORT BUTTON
Private Sub CommandButton2_Click()

    '--newly add--Test if mandatory is blank/empty
    If IsEmpty(Range("I4").Value) = True Then
        MsgBox "input missing"
   
    Else

        CreateObject("Wscript.Shell").popup "Please allow a few second for Exporting... ", 1, "Kind Reminder", vbSystemModal + 64
 
        Dim sourceSheet As Worksheet
        Set sourceSheet = ActiveSheet

        Application.ScreenUpdating = False

        ' Open Source and Destination files

        ChDir " folder path"
        Workbooks.Open Filename:="workbook2.xlsx"
   
        ' Copy Dedicated Part
 
        Workbooks("workbook1.xlsm").Sheets("sheet name").Range("range").Copy
        Workbooks("workbook2.xlsx").Sheets("sheet name").Range("A1").PasteSpecial xlPasteValues

        Application.CutCopyMode = False
        Application.Goto Worksheets("sheet name").Range("A1"), True

        ' Save Destination file named as Today's date

        DD = Day(Date)
        MMM = Month(Date)
        YY = Year(Date)
        WBK = "workbook"
 
        Windows("workbook2").Activate
        ActiveWorkbook.SaveAs Filename:=WBK, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
 
        ActiveWindow.Close
 
        Call sourceSheet.Activate

        MsgBox "done", vbOKOnly + vbInformation, "Export Completed"
 
    End If
End Sub
 
Upvote 0
Thank you. I am now able to make multiple check with few "if-then-else".
It works perfectly like I wanted.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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