Thursday, June 11, 2020

VBA Function to Clear content except header rows and Get col headers in string or array

Folks,

sometimes need to use only headers or template from existing file then below code might help to clear the data. Sometimes you need to capture the column header in array or string then below function will help you.

Function DeleteExceptFirstHeader(shWrite)
    'shWrite.Rows("2:" & Rows.Count).ClearContents
    shWrite.Rows("2:" & Rows.Count).Clear
End Function

Function fnReadColHeader(wST As Worksheet)
    Dim strData() As Variant
    lastCol = wST.Range("a1").End(xlToRight).Column
    strData = wST.Range("1:" & lastCol).Value
    strHeaders = 0
    Dim columnCounter As Long
 
    For columnCounter = LBound(strData, 1) To UBound(strData, 1)
        strHeaders = strHeaders & ":" & strData(1, columnCounter)
    Next columnCounter
 '   MsgBox strHeaders
   fnReadColHeaderDRF = strHeaders
End Function

Cheers.
TJ

No comments:

Post a Comment