VBA - How to copy and paste the entire column based on a condition?

nevers711

New Member
Joined
Oct 11, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi everyone, I am pretty new to VBA. Can anybody help me with my code?

I have an excel file, and sheet1 contains the source data. Please see attached
Capture.JPG


I would like to use Macro to help me copy the entire column of "PO #", and paste the entire "PO #"column to Sheet2's cell A1.. The position of "PO #" may vary. It may not be at Column C, so I need to ask Macro to help me locate that column

Here is the code I wrote but it doesn't work.. It only copies the column where my mouse is at... not sure what is wrong



Sub Macro1()

Dim j As Long
For j = 1 To 8
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(j, 1) = "PO #" Then Sheets("Sheet1").Cells(j, 1).Select
Selection.EntireColumn.Copy
Sheets("Sheet2").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next j

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
you are looping through rows instead of columns and all the line of codes for copying and pasting should be placed inside if function

i have only altered what was most needed this can be more trimmed down.


Sub Macro1()

Dim j As Long
For j = 1 To 8
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, j) = "PO #" 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
End If
Next j

End Sub
 
Upvote 0
Solution
you are looping through rows instead of columns and all the line of codes for copying and pasting should be placed inside if function

i have only altered what was most needed this can be more trimmed down.


Sub Macro1()

Dim j As Long
For j = 1 To 8
Sheets("Sheet1").Activate
If Sheets("Sheet1").Cells(1, j) = "PO #" 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
End If
Next j

End Sub
OMG! Thank you so much for finding this out!! I didn't notice this issue!!
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top