Trouble Copying Columns from One Sheet to Another Using VBA

monicamarie

Board Regular
Joined
Sep 2, 2008
Messages
66
Hello.
I have a worksheet (Sheet1) with 8 columns of data starting on Row 1. The column names are Cust Number, Created On, Account, Problem, State, ST, System Type and Ticket. (Not always in that order)
The code below is used to copy specific columns from sheet 1 to a new created worksheet called Service Queue.
The problem is that for some reason, it's not copying the column named "ST" from Sheet 1 but instead it's copying a column called "State" Can someone help me to fix this issue?

Sub MoveColumns()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Integer

Sheets.Add.Name = "Service Queue"
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Service Queue")
myColumns = Array("Cust Number", "Created On", "ST")


With ws1.Range("A1:H1")
For i = 0 To UBound(myColumns)
On Error Resume Next
.Find(myColumns(i)).EntireColumn.Copy Destination:=ws2.Cells(1, i + 1)
Err.Clear
Next i
End With
Set ws1 = Nothing
Set ws2 = Nothing


End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,119
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
That's what happens when you don't use the various paremeters of Find I'm afraid (it will use whatever it's previous settings were).
Try...
VBA Code:
.Find(What:=myColumns(i), LookAt:=xlWhole).EntireColumn.Copy Destination:=ws2.Cells(1, i + 1)
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,119
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
You're welcome
 

Watch MrExcel Video

Forum statistics

Threads
1,132,970
Messages
5,656,160
Members
418,283
Latest member
Venkateswara Rao

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
Top