Sub Generate_Report()
Sheets(2).Range("A3:L1000").Clear
For lngRow = 3 To Sheets(1).Cells(3, "A").End(xlDown).Row
lngReportRow = Sheets(2).Cells(65536, "A").End(xlUp).Row + 1
If lngReportRow = 2 Then lngReportRow = 3
Sheets(2).Cells(lngReportRow, "A").Value = Sheets(1).Cells(lngRow, "B").Text
lngTargetCol = FindTargetCol(Sheets(1).Cells(lngRow, "D").Value)
If lngTargetCol > 0 Then
Sheets(2).Cells(lngReportRow, lngTargetCol).Value = Sheets(1).Cells(lngRow, "G").Text
Else
MsgBox "Cannot find column for " & Sheets(1).Cells(lngRow, "D").Value & " from row " & lngRow
End If
Sheets(2).Cells(lngReportRow, "J").Value = Sheets(1).Cells(lngRow, "E").Text
Sheets(2).Cells(lngReportRow, "K").Value = Sheets(1).Cells(lngRow, "F").Text
Sheets(2).Cells(lngReportRow, "L").Value = Sheets(1).Cells(lngRow, "L").Text
Next
End Sub
Private Function FindTargetCol(strColumnName)
lngFoundCol = 0
For lngCol = 2 To 9
If LCase(Sheets("Report").Cells(2, lngCol).Value) = LCase(strColumnName) Then
lngFoundCol = lngCol
Exit For
End If
Next
FindTargetCol = lngFoundCol
End Function
|