PDA

View Full Version : Help - VBA to automatically reorder columns



jsabo
11-07-2013, 12:26 PM
Hello,

Can anyone help me with code will automatically look at columns in an excel sheet and reorder them in a pre-specified order? We already have code that kicks out an error if the correct columns aren't in the excel file, but it has no concern over the order of them. Also, it is preferred that the code takes into account that there may be slight variances in column name - for instance, one group at my company may use a column named "Supplier Name", while another uses a column named "Supplier_Name". Any ideas?

Thanks,

Jon S

patel
11-09-2013, 12:21 AM
the question is too vague for me, attach a sample file with current data and desired data, you current code included

p45cal
11-09-2013, 02:00 PM
You could try something along these lines, where the line beginning For Each Header.. has the column headers in the order you want, but include the variations in name in the list:

Sub Macro12()
DestCol = 0
For Each Header In Array("Head1", "Head2", "Head3", "Head4", "Head5", "Head_5", "Head7")
x = Application.Match(Header, Rows(1), 0)
If Not IsError(x) Then
DestCol = DestCol + 1
If x <> DestCol Then
Columns(x).Cut
Columns(DestCol).Insert
End If
End If
Next Header
End Sub

jsabo
11-11-2013, 02:10 PM
After a little tweaking, got it working. Thanks for your help!