I have a worksheet where I copy/paste columns into a template (in this case Brad.xlsx columns A, B, L, Z) by using the below macro. It's opening the template document but not copying the columns. Would someone please help me make this work? The columns in the original worksheet aren't always in this order.
Sub CHCopyMatchingColumns()
' CHCopyMatchingColumns Macro
' Copy SPA into pdem template
Dim head_count As Integer
Dim row_count As Integer
Dim Col_count As Integer
Dim i As Integer
Dim j As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Count headers in this workbook
head_count = WorksheetFunction.CountA(ws.Range("A1", ws.Range("A1").End(xlToRight)))
' Open other workbook and count rows and columns
Workbooks.Open FileName:="G:\REBATES\CutlerHammer\PDEM load\test\template.xlsx"
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.Sheets(1).Activate
row_count = WorksheetFunction.CountA(activeSheet.Range("A1", activeSheet.Range("A1").End(xlDown)))
Col_count = WorksheetFunction.CountA(activeSheet.Range("A1", activeSheet.Range("A1").End(xlToRight)))
For i = 1 To head_count
j = 1
Do While j <= Col_count
If ws.Cells(1, i).Value = activeSheet.Cells(1, j).Text Then
ws.Range(ws.Cells(1, i), ws.Cells(ws.Cells(ws.Rows.Count, i).End(xlUp).Row, i)).Copy
activeSheet.Cells(1, j).PasteSpecial xlPasteValues
Application.CutCopyMode = False
j = Col_count
End If
j = j + 1
Loop
Next i
End Sub
Sub CHCopyMatchingColumns()
' CHCopyMatchingColumns Macro
' Copy SPA into pdem template
Dim head_count As Integer
Dim row_count As Integer
Dim Col_count As Integer
Dim i As Integer
Dim j As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Count headers in this workbook
head_count = WorksheetFunction.CountA(ws.Range("A1", ws.Range("A1").End(xlToRight)))
' Open other workbook and count rows and columns
Workbooks.Open FileName:="G:\REBATES\CutlerHammer\PDEM load\test\template.xlsx"
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.Sheets(1).Activate
row_count = WorksheetFunction.CountA(activeSheet.Range("A1", activeSheet.Range("A1").End(xlDown)))
Col_count = WorksheetFunction.CountA(activeSheet.Range("A1", activeSheet.Range("A1").End(xlToRight)))
For i = 1 To head_count
j = 1
Do While j <= Col_count
If ws.Cells(1, i).Value = activeSheet.Cells(1, j).Text Then
ws.Range(ws.Cells(1, i), ws.Cells(ws.Cells(ws.Rows.Count, i).End(xlUp).Row, i)).Copy
activeSheet.Cells(1, j).PasteSpecial xlPasteValues
Application.CutCopyMode = False
j = Col_count
End If
j = j + 1
Loop
Next i
End Sub