Check in on specific sheet (specific range) if isEmpty or not before closing workbook

Gwhaou

Board Regular
Joined
May 10, 2022
Messages
78
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi,

Actually I'm trying to initialize my workbook before closing it (by deleting all the added data) save it then close it.
Before that I want to check if there is any data on a specific sheet before deleting and closing the workbook, so if there is any data on that particular sheet so i can stop the deleting and closing process.
I tried this code but even when the specifc sheet is Empty it close the workbook

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

'In case if that particular sheet is empty
'Delete all the data form other sheet then save and close the workbook
    If IsEmpty(Worksheets("DATA_SHEET").Range("A2:Q50")) = True Then

        Worksheets("SHEET1").Rows(2 & ":" & Worksheets("SHEET1").Rows.Count).Delete
        Worksheets("SHEET2").Rows(2 & ":" & Worksheets("SHEET2").Rows.Count).Delete
        ActiveWorkbook.Save
        ThisWorkbook.Saved = True

'In case that the particular sheet is not empty 
'Active that sheet and cancel  the closing process       
    Else

        Worksheets("DATA_SHEET").Activate
        MsgBox "Carful there is data on the actual sheet !"
      

    End If
    

End Sub

Every time i click on X for closing the workbook the code is opening the message box, even if there is data or not one the "DATA_SHEET"

1662552795764.png

Even when i Click on OK or X it close the work book.



I want some Help please 🙏
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
IsEmpty does not tell you whether the sheet is empty. It tells you if a variable is uninitialized. The way you are using it, it will always be FALSE. Try this:

VBA Code:
    If WorksheetFunction.CountA(Worksheets("DATA_SHEET").Range("A2:Q50")) = 0 Then
 
Upvote 0
Solution
IsEmpty does not tell you whether the sheet is empty. It tells you if a variable is uninitialized. The way you are using it, it will always be FALSE. Try this:

VBA Code:
    If WorksheetFunction.CountA(Worksheets("DATA_SHEET").Range("A2:Q50")) = 0 Then
Ok i'm gonna try that, thks for the reply 👌
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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