Procedure before my code if some sheets doesn't contain data

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
245
Office Version
  1. 2019
  2. 2010
Hi
I search for procedure to put before my code .
if there are some sheets don't contain data especially in row1 even if the sheet contains data in any cell exept the row1 then populate message box " sorry you can't implement the code and some sheets don't contain headers in row1"
and exit sub
thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Something like this perhaps.
VBA Code:
Sub CheckSheetsForHeaders()
    Dim ws As Worksheet
    Dim hasDataInRow1 As Boolean
    Dim hasHeadersInRow1 As Boolean
    
    hasDataInRow1 = False
    hasHeadersInRow1 = False
    
    For Each ws In ThisWorkbook.Worksheets
        If Application.WorksheetFunction.CountA(ws.Rows(1)) > 0 Then
            hasDataInRow1 = True
            If Application.WorksheetFunction.CountA(ws.Rows(1)) = ws.UsedRange.Columns.Count Then
                hasHeadersInRow1 = True
            Else
                MsgBox "Sheet '" & ws.Name & "' doesn't contain headers in row 1."
                Exit Sub
            End If
        End If
    Next ws
    
    If Not hasDataInRow1 Then
        MsgBox "No sheet contains data in row 1."
        Exit Sub
    End If
    
    ' If code execution reaches here, it means all sheets contain data in row 1 with headers
    ' Put your main code here
    
End Sub
 
Upvote 0
Thanks
unfortunately doesn't show message as you did in the code!
it runs my code even if some sheets are empty in row1.
 
Upvote 0
Try this:
VBA Code:
Sub Check_Blank_Sheets()
    Dim ws As Worksheet
    Dim Flag As Boolean
    Dim c As Range
    Dim tx As String
    For Each ws In ThisWorkbook.Worksheets
        Set c = ws.Rows(1).Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
        If c Is Nothing Then
            tx = tx & vbLf & ws.Name
            Flag = True
        End If
    Next ws
    
    If Flag Then
        MsgBox "First row of this sheet(s) is blank: " & tx
        Exit Sub
    End If
   
    'Put your code here
    
End Sub
 
Upvote 0
@Akuini
really great !
sorry I'm not clear from th bginning !🙏🙏
I would if there is no headers A:F or just header in any column(A1:B1) or( A1,C1) or( A1,C1,D1 )then should show the message .
if the whole range A1:F1 contains header then run my code .
 
Upvote 0
if the whole range A1:F1 contains header then run my code .
Try:
VBA Code:
Sub Check_Blank_Sheets_2()
    Dim ws As Worksheet
    Dim Flag As Boolean
    Dim tx As String
    For Each ws In ThisWorkbook.Worksheets
        If WorksheetFunction.CountA(ws.Range("A1:F1")) < 6 Then
            tx = tx & vbLf & ws.Name
            Flag = True
        End If
    Next ws
    
    If Flag Then
        MsgBox "Some headers of this sheet(s) is blank: " & tx
        Exit Sub
    End If
   
    'Put your code here
    
End Sub
 
Upvote 0
Solution
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,845
Members
449,471
Latest member
lachbee

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