【VBA】获取某列名为表头所在列的列号,获取某列中行表头的所在行的行号


1. 获取表头的列号,可直接调用函数,传入参数,因为是表头,所以rowID为 1 

2. 获取第一列中行表头的所在行,传入参数时,colID 为 1

'获取列号
Public Function Find_Col_ID(ByVal rowID, ByVal objWorkBook, ByVal objWorkSheet, ByVal strColName) As Integer
    objWorkBook.Activate
    objWorkSheet.Select
    objWorkSheet.Cells(1, 1).Select
    Cells.Find(what:=strColName, after:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Select
    If Selection.Row = rowID Then
        Find_Col_ID = Selection.Column
    Else
        Find_Col_ID = 0
    End If

End Function
'获取行号
Public Function Find_Row_ID(ByVal colID, ByVal objWorkBook, ByVal objWorkSheet, ByVal strName) As Integer
    objWorkBook.Activate
    objWorkSheet.Select
    objWorkSheet.Cells(1, 1).Select
    Cells.Find(what:=strName, after:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Select
    If Selection.Columns = colID Then
        Find_Row_ID = Selection.Rows
    Else
        Find_Row_ID = 0
    End If

End Function