youtube macro gone wrong

Tonyk1051

Board Regular
Joined
Feb 1, 2021
Messages
132
Office Version
  1. 2019
Platform
  1. Windows
Hi,


So i needed a macro that could help me re arrange a bunch of coulmns to a certain order instead of having to do it manually cut and paste all the time. I found a good youtube that did what i was looking for exactly video but in the end it didnt work... i would message the youtuber but im sure asking here would get me much faster results than having to wait on youtube. Please help



Code:
 Sub vds()

Dim new_column_order As Variant, new_index As Integer

Dim found As Range, counter As Range

new_column_order = Array("Item ID", "Vendor Type", "Kit Flag", "Source", "Vendor", "Vendor Liasion", "Manufacturer", "RTV Category", "Date RTV Category", "User RTV Category", "Shelf", "Prior Category", "Product Group", "Booknet", "warehouse", "has_lithium", "openbox", "dfi", "Date Received", "Customer Purchase Date", "RMA Date", "Defect Cat", "Defect Desc", "Serial No", "Item Code", "Cust Flag", "catlgno", "buyer", "brand", "Description", "Long Catalog No", "PO", "Is Used", "Multi Vendor Flag", "Last Vendor", "Return Reason 1")


counter = 1


For new_index = LBound(new_column_order) To UBound(new_column_order)


Set found = Rows("1:1").Find(new_column_order(new_index), LookIn:=xlValues, _

lookat:=xlWhole, searchorder:=xlByColumns, searchdirections:=xlNext, MatchCase:=False)


If Not found Is Nothing Then

If found.Column <> counter Then

found.EntireColumn.Cut

Columns(counter).Insert shift:=xlToRight

End If



counter = counter + 1

End If



Next new_index



End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,​
as a faster way is to just enter the headers row with the new order without any typo in the headers names​
then just using an advanced filter so it can be achieved without any VBA procedure !​
If for some reason a VBA procedure is really necessary the same way can be used with so few codelines,​
often only two so more efficient than any poor youtube video …​
 
Upvote 0
Solution
You need to declare counter as Long, not Range.

And it's SearchDirection, not SearchDirections.
 
Upvote 0
Hi,​
as a faster way is to just enter the headers row with the new order without any typo in the headers names​
then just using an advanced filter so it can be achieved without any VBA procedure !​
If for some reason a VBA procedure is really necessary the same way can be used with so few codelines,​
often only two so more efficient than any poor youtube video …​

Hi,​
as a faster way is to just enter the headers row with the new order without any typo in the headers names​
then just using an advanced filter so it can be achieved without any VBA procedure !​
If for some reason a VBA procedure is really necessary the same way can be used with so few codelines,​
often only two so more efficient than any poor youtube video …​
holy cow you were right with the advanced filter, thanks a million man
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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