Hello everyone,
I wrote some Macros and would like it to help me copy and paste the entire columns based on the header.
Sheet1 is the source data, and I would like Macros to help me copy the entire column if the headers are "PO #", "Vendor Name", "Service Start Date", "Service End Date", and "Outstanding amount" from sheet 1 to sheet2. Here is my code:
Sub Macro1()
'
' Macro1 Macro
'
Dim j As Long
For j = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, j) = "PO #" Or Sheets("Sheet1").Cells(1, j) = "PO#" Or Sheets("Sheet1").Cells(1, j) = "PO Number" Then Sheets("Sheet1").Cells(1, j).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next j
Dim k As Long
For k = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, k) = "Vendor Name" Then Sheets("Sheet1").Cells(1, k).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next k
Dim i As Long
For i = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, i) = "service start date" Then Sheets("Sheet1").Cells(1, i).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next i
Dim m As Long
For m = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, m) = "service end date" Then Sheets("Sheet1").Cells(1, m).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next m
Dim n As Long
For n = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, n) = "Outstanding Amount" Then Sheets("Sheet1").Cells(1, n).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("E1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next n
End Sub
Every time I run it, it incorrectly copied and pasted "Vendor Name" column to "service start date" & "service end date" columns.. Does anybody know why? I double checked and I am pretty sure I didn't spell anything wrong and all the service start or end dates are there in sheet1. Also, can anybody help me improve these codes since I think it is pretty slow to run.. Thank you so much!
I wrote some Macros and would like it to help me copy and paste the entire columns based on the header.
Sheet1 is the source data, and I would like Macros to help me copy the entire column if the headers are "PO #", "Vendor Name", "Service Start Date", "Service End Date", and "Outstanding amount" from sheet 1 to sheet2. Here is my code:
Sub Macro1()
'
' Macro1 Macro
'
Dim j As Long
For j = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, j) = "PO #" Or Sheets("Sheet1").Cells(1, j) = "PO#" Or Sheets("Sheet1").Cells(1, j) = "PO Number" Then Sheets("Sheet1").Cells(1, j).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next j
Dim k As Long
For k = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, k) = "Vendor Name" Then Sheets("Sheet1").Cells(1, k).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next k
Dim i As Long
For i = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, i) = "service start date" Then Sheets("Sheet1").Cells(1, i).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next i
Dim m As Long
For m = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, m) = "service end date" Then Sheets("Sheet1").Cells(1, m).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next m
Dim n As Long
For n = 1 To 100
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, n) = "Outstanding Amount" Then Sheets("Sheet1").Cells(1, n).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("E1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next n
End Sub
Every time I run it, it incorrectly copied and pasted "Vendor Name" column to "service start date" & "service end date" columns.. Does anybody know why? I double checked and I am pretty sure I didn't spell anything wrong and all the service start or end dates are there in sheet1. Also, can anybody help me improve these codes since I think it is pretty slow to run.. Thank you so much!