ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Finding/Moving Columns within Macro (https://www.excelbanter.com/excel-programming/420407-finding-moving-columns-within-macro.html)

Jake[_5_]

Finding/Moving Columns within Macro
 

So I need some advice/direction on how to perform this macro a little
more efficiently.
Everyday I export a report from our POS system at work into Excel. The
issue with the export is that none of the columns are in order so I
have to manually move the columns each time or create a macro to do
it. Currently I'm using the below code to find the column header and
move it to the correct location.

Dim intWarehouse

intWarehouse = Cells.Find(What:="Warehouse").Column
Columns(intWarehouse).Select
Selection.Cut Destination:=Columns("A:A")

I only have 15 columns that I need to find and then move but I would
think there has to be a more efficient way of doing this than creating
15 variables and using 3 lines of code for each column that I need to
find then move. For some reason I think I should be using an array but
after doing some research on arrays I'm even more confused on where to
start.

Is there a better way to do this? If so, can you point me in that
direction?

Thanks in advance -
Jake

Per Jessen[_2_]

Finding/Moving Columns within Macro
 
Hi Jake

Try this:

Sub Find_Move()
Dim MyArr As Variant
Dim ColArr As Variant
Dim TargetCol As Long

MyArr = Array("Item1", "Item2", "Item3") ' Change to suit
ColArr = Array("A:A", "B:B", "C:C") ' Destination column

For i = LBound(MyArr) To UBound(MyArr)
TargetCol = Cells.Find(What:=MyArr(i)).Column
Columns(TargetCol).Cut Destination:=Columns(ColArr(i))
Next
End Sub

Regards,
Per

On 22 Nov., 18:22, Jake wrote:
So I need some advice/direction on how to perform this macro a little
more efficiently.
Everyday I export a report from our POS system at work into Excel. The
issue with the export is that none of the columns are in order so I
have to manually move the columns each time or create a macro to do
it. Currently I'm using the below code to find the column header and
move it to the correct location.

Dim intWarehouse

intWarehouse = Cells.Find(What:="Warehouse").Column
Columns(intWarehouse).Select
Selection.Cut Destination:=Columns("A:A")

I only have 15 columns that I need to find and then move but I would
think there has to be a more efficient way of doing this than creating
15 variables and using 3 lines of code for each column that I need to
find then move. For some reason I think I should be using an array but
after doing some research on arrays I'm even more confused on where to
start.

Is there a better way to do this? If so, can you point me in that
direction?

Thanks in advance -
Jake



franciz

Finding/Moving Columns within Macro
 
Have you try Excel's Text to Columns function under Data on the Menu bar?
This sound like a candidate for it

hope this help.
click on Yes if this help

thanks

"Jake" wrote:


So I need some advice/direction on how to perform this macro a little
more efficiently.
Everyday I export a report from our POS system at work into Excel. The
issue with the export is that none of the columns are in order so I
have to manually move the columns each time or create a macro to do
it. Currently I'm using the below code to find the column header and
move it to the correct location.

Dim intWarehouse

intWarehouse = Cells.Find(What:="Warehouse").Column
Columns(intWarehouse).Select
Selection.Cut Destination:=Columns("A:A")

I only have 15 columns that I need to find and then move but I would
think there has to be a more efficient way of doing this than creating
15 variables and using 3 lines of code for each column that I need to
find then move. For some reason I think I should be using an array but
after doing some research on arrays I'm even more confused on where to
start.

Is there a better way to do this? If so, can you point me in that
direction?

Thanks in advance -
Jake


JMay

Finding/Moving Columns within Macro
 
Per, Your code is neat, but how can one "equate" the Source columns to
your examples "Items1", "Items2", etc ?
TIA,



"Per Jessen" wrote:

Hi Jake

Try this:

Sub Find_Move()
Dim MyArr As Variant
Dim ColArr As Variant
Dim TargetCol As Long

MyArr = Array("Item1", "Item2", "Item3") ' Change to suit
ColArr = Array("A:A", "B:B", "C:C") ' Destination column

For i = LBound(MyArr) To UBound(MyArr)
TargetCol = Cells.Find(What:=MyArr(i)).Column
Columns(TargetCol).Cut Destination:=Columns(ColArr(i))
Next
End Sub

Regards,
Per

On 22 Nov., 18:22, Jake wrote:
So I need some advice/direction on how to perform this macro a little
more efficiently.
Everyday I export a report from our POS system at work into Excel. The
issue with the export is that none of the columns are in order so I
have to manually move the columns each time or create a macro to do
it. Currently I'm using the below code to find the column header and
move it to the correct location.

Dim intWarehouse

intWarehouse = Cells.Find(What:="Warehouse").Column
Columns(intWarehouse).Select
Selection.Cut Destination:=Columns("A:A")

I only have 15 columns that I need to find and then move but I would
think there has to be a more efficient way of doing this than creating
15 variables and using 3 lines of code for each column that I need to
find then move. For some reason I think I should be using an array but
after doing some research on arrays I'm even more confused on where to
start.

Is there a better way to do this? If so, can you point me in that
direction?

Thanks in advance -
Jake





All times are GMT +1. The time now is 05:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com