Finding out The HeaderRow number of ActiveSheet

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hi

I am trying to find out the Header Row of ActiveSheet but somehow not able to succeed as few errors generated while trying the beautiful code adopted from

Will be highly appreciated if someone could correct me and helping to find the Activesheet or Activesheet.Name somewhere appropraitely activesheet needs to be defined

MsgBox findHeaderRow(Sheet4)------> this works perfectly as per the code below It gives you Header Row Number of Sheet 4
but when tried with below 2 syntaxes got the following errors
MsgBox findHeaderRow(ActiveSheet.Name) getting error Object required
MsgBox findHeaderRow(curSheetName) getting Error Byref argument type mismatch


VBA Code:
Private Sub cmdGetHdrRowNo_Click()
Dim wks As Excel.Worksheet
Dim strSheetName As String

'Dim curSheetName As String
curSheetName = ActiveSheet.Name

'Set wks = Application.ActiveSheet
'curSheetName = ActiveSheet.Name
'curSheetName = wks.Name[/I][/B]


MsgBox  findHeaderRow(Sheet4)------> this works perfectly as per the code below It gives you Header Row Number of Sheet 4'
''MsgBox  findHeaderRow(ActiveSheet.Name)   getting error Object requred
''MsgBox  findHeaderRow(curSheetName) getting Error Byref argument type mismatch

End Sub

Public Function findHeaderRow(wks As Excel.Worksheet) As Long
Const METHOD_NAME As String = "findHeaderRow"
    Dim lastColumn As Long
  
    If Not isSheetValid(wks) Then GoTo IllegalSheetException                                          

    lastColumn = lastNonEmptyColumn(wks)
    findHeaderRow = firstNonEmptyRow(wks:=wks, startCol:=lastColumn, endCol:=lastColumn)

ExitPoint:
    Exit Function

IllegalSheetException:
    GoTo ExitPoint

End Function

Public Function lastNonEmptyColumn(wks As Excel.Worksheet, _
                   Optional startRow As Long, Optional startCol As Long, _
                   Optional endRow As Long, Optional endCol As Long, _
                   Optional ignoreHiddenCells As Boolean = False) As Long
   
    Const METHOD_NAME As String = "lastNonEmptyColumn"
    Dim lngCol As Long
    Dim lngColStart As Long
    Dim lngColEnd As Long
    Dim lngNonBlanks As Long
    Dim lngRowStart As Long
    Dim lngRowEnd As Long
    Dim rng As Excel.Range

    If Not isSheetValid(wks) Then GoTo IllegalSheetException                                          

    If startCol > 0 And startCol <= wks.Columns.Count Then lngColStart = startCol Else lngColStart = 1
    If endCol > 0 And endCol <= wks.Columns.Count Then lngColEnd = endCol Else _
                                                                        lngColEnd = wks.Columns.Count 
    If startRow > 0 And startRow <= wks.Rows.Count Then lngRowStart = startRow Else lngRowStart = 1   
    If endRow > 0 And endRow <= wks.Rows.Count Then lngRowEnd = endRow Else lngRowEnd = wks.Rows.Count

Retry:
    lngCol = 1

    Do                                                                                                
        Set rng = wks.Range(wks.Cells(lngRowStart, lngCol), wks.Cells(lngRowEnd, lngColEnd))          
        lngNonBlanks = Excel.Application.WorksheetFunction.CountA(rng)                                
                                                                                                      
        If lngNonBlanks Then                                                                       
                                                                                                   
            If lngCol = lngColEnd Then Exit Do                                                     
            lngColStart = lngCol                                                                   
            lngCol = lngCol + ((lngColEnd - lngCol + 1) / 2)                                       
                                                                                                   
        Else                                                                                       
            lngColEnd = lngCol - 1                                                                 
            lngCol = lngColStart                                                                   
                                                                                                   
            If lngColStart > lngColEnd Then                                                     
                lngCol = 0                                                                      
                Exit Do                                                                         
            End If                                                                              
                                                                                                   
                                                                                                   
        End If                                                                                     
                                                                                                     
    Loop                                                                                              

    If lngCol Then                                                                                    
                                                                                                      
        If ignoreHiddenCells And wks.Columns(lngCol).Hidden Then                                   
            lngColEnd = nextVisibleColumn(wks, lngCol, Excel.xlToLeft)                             
            If lngColEnd Then GoTo Retry                                                           
        Else                                                                                       
            lastNonEmptyColumn = lngCol                                                            
        End If                                                                                     
                                                                                                      
    End If                                                                                            

ExitPoint:
    Exit Function

IllegalSheetException:
    GoTo ExitPoint


End Function

Public Function isSheetValid(wks As Excel.Worksheet) As Boolean
    Const METHOD_NAME As String = "isSheetValid"
    Dim strSheetName As String
  
   
    On Error Resume Next
   
    strSheetName = wks.Name

    isSheetValid = VBA.Len(strSheetName)

End Function

Public Function nextVisibleColumn(wks As Excel.Worksheet, initialCol As Long, _
                                          direction As XlDirection) As Long
    Const METHOD_NAME As String = "nextVisibleColumn"
    Dim intOffset As Integer

Set wks = Application.ActiveSheet 'added by Sam

    If Not isSheetValid(wks) Then GoTo IllegalSheetException                                          

    Select Case direction                                                                             
        Case Excel.xlToLeft:    intOffset = -1                                                        
        Case Excel.xlToRight:   intOffset = 1                                                         
        Case Else                                                                                     
            nextVisibleColumn = initialCol                                                            
            GoTo ExitPoint                                                                            
    End Select                                                                                        

    nextVisibleColumn = initialCol                                                                    
    Do                                                                                                
        nextVisibleColumn = nextVisibleColumn + intOffset                                             
        If Not wks.Columns(nextVisibleColumn).Hidden Then Exit Do                                     
    Loop                                                                                              

ExitPoint:
    Exit Function

IllegalSheetException:
    GoTo ExitPoint

End Function

Public Function firstNonEmptyRow(wks As Excel.Worksheet, _
                      Optional startRow As Long, Optional startCol As Long, _
                      Optional endRow As Long, Optional endCol As Long, _
                      Optional ignoreHiddenCells As Boolean = False) As Long
    Const METHOD_NAME As String = "firstNonEmptyRow"

    Dim lngRow As Long
    Dim lngRowStart As Long
    Dim lngRowEnd As Long
    Dim lngNonBlanks As Long
    Dim lngColStart As Long
    Dim lngColEnd As Long
    Dim rng As Excel.Range


    If Not isSheetValid(wks) Then GoTo IllegalSheetException                                          
    If startCol > 0 And startCol <= wks.Columns.Count Then lngColStart = startCol Else lngColStart = 1
    If endCol > 0 And endCol <= wks.Columns.Count Then lngColEnd = endCol Else _
                                                                        lngColEnd = wks.Columns.Count 
    If startRow > 0 And startRow <= wks.Rows.Count Then lngRowStart = startRow Else lngRowStart = 1   
    If endRow > 0 And endRow <= wks.Rows.Count Then lngRowEnd = endRow Else lngRowEnd = wks.Rows.Count

Retry:
    If endRow > 0 And endRow <= wks.Rows.Count Then                                                   
        lngRow = endRow                                                                               
    Else                                                                                              
        lngRow = wks.Rows.Count                                                                       
    End If                                                                                            
    Do                                                                                                
        Set rng = wks.Range(wks.Cells(lngRowStart, lngColStart), wks.Cells(lngRow, lngColEnd))        
        lngNonBlanks = Excel.Application.WorksheetFunction.CountA(rng)                                

        If lngNonBlanks Then                                                                       
                                                                                                   
            If lngRow = lngRowStart Then Exit Do                                                   
            lngRowEnd = lngRow                                                                     
            lngRow = lngRowStart + ((lngRow - lngRowStart - 1) / 2)                                
                                                                                                   
        Else                                                                                       
            lngRowStart = lngRow + 1                                                               
            lngRow = lngRowEnd                                                                     
                                                                                                   
            If lngRowStart > lngRowEnd Then                                                     
                lngRow = 0                                                                      
                Exit Do                                                                         
            End If                                                                              
                                                                                                   
        End If                                                                                     
                                                                                                      
    Loop                                                                                              

    If lngRow Then                                                                                    
                                                                                                      
        If ignoreHiddenCells And wks.Rows(lngRow).Hidden Then                                      
            lngRowStart = nextVisibleRow(wks, lngRow, Excel.xlDown)                                
            If lngRowStart <= wks.Rows.Count Then GoTo Retry                                       
        Else                                                                                       
            firstNonEmptyRow = lngRow                                                              
        End If                                                                                     
                                                                                                    
    End If                                                                                            

ExitPoint:
    Exit Function

IllegalSheetException:
    GoTo ExitPoint

End Function

Public Function nextVisibleRow(wks As Excel.Worksheet, initialRow As Long, _
                                                                          direction As XlDirection) As Long
    Const METHOD_NAME As String = "nextVisibleRow"
    Dim intOffset As Integer

    If Not isSheetValid(wks) Then GoTo IllegalSheetException                                          

    Select Case direction                                                                             
        Case Excel.xlUp:    intOffset = -1                                                            
        Case Excel.xlDown:  intOffset = 1                                                             
        Case Else                                                                                     
            nextVisibleRow = initialRow                                                               
            GoTo ExitPoint                                                                            
    End Select                                                                                        

    nextVisibleRow = initialRow                                                                       
    Do                                                                                                
        nextVisibleRow = nextVisibleRow + intOffset                                                   
        If Not wks.Rows(nextVisibleRow).Hidden Then Exit Do                                           
    Loop                                                                                              

ExitPoint:
    Exit Function

IllegalSheetException:
    GoTo ExitPoint

End Function
Thanks
SamD
122
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi,
just pass the sheet object to your function

VBA Code:
MsgBox findHeaderRow(ActiveSheet)

and if getting ByRef argument error try passing your argument ByVal

Rich (BB code):
Public Function findHeaderRow(ByVal wks As Excel.Worksheet) As Long

and see if any of this helps you

Dave
 
Upvote 0
Dave

Thanks for the Suggestion. Only one change and indeed got resolved
Rich (BB code):
Public Function findHeaderRow(ByVal wks As Excel.Worksheet) As Long
FYI

rest of the functions i let it be as it is without passing arguement ByVal

Thank you so much

SamD :)?
123
 
Upvote 0
Dave

Thanks for the Suggestion. Only one change and indeed got resolved
Rich (BB code):
Public Function findHeaderRow(ByVal wks As Excel.Worksheet) As Long
FYI

rest of the functions i let it be as it is without passing arguement ByVal

Thank you so much

SamD :)?
123
 
Upvote 0
most welcome - glad suggestion resolved issue for you

Dave
 
Upvote 0

Forum statistics

Threads
1,215,442
Messages
6,124,886
Members
449,194
Latest member
ronnyf85

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