I having a trouble in getting the column value of a header name. I tried to search online but i cant find any solution. The column value is need to be use to copy the data from a sheet to another sheet.
your help is greatly appreciated
Sub CopyCols()
Dim h As Range, f As Range, sht1, rngDest As Range
Dim ColNum As Integer
Dim lastRow As Long
Set sht1 = ThisWorkbook.Sheets("Data")
Set rngDest = ThisWorkbook.Sheets("DataDb").Range("A1") 'start pasting here
'loop through the headers to copy
For Each h In sht1.Range("N1:AJ1").Cells
'find the header on sheet1
Set f = sht1.Rows(4).Find(what:=h, lookat:=xlPart)
ColNum = f.Column
If Not f Is Nothing Then
'found the header: copy the column
lastRow = sht1.Cells(Rows.Count, ColNum).End(xlUp).Row
Sheet2.Range(Cells(4, ColNum), Cells(lastRow, ColNum)).Copy
rngDest.PasteSpecial Paste:=xlValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If
Set f = Nothing
Next h
End Sub