Column reordering with VBA

Jimbob2000

New Member
Joined
Jun 27, 2019
Messages
25
I have the following code as part of a macro to reformat a spreadsheet when transferring information between two databases at a children's nonprofit where I work.

This part of the code is supposed to reorder the columns, and it works.... about half the time.

When it doesn't work, the debug function always highlights the row "Columns(counter).Insert Shift:=xlToRight" and it seems to get as far as moving the "Zip Code" column, but I don't understand this bit of code well enough to see what's going wrong. It always crashes excel when it doesn't work, which sucks.

Any help or suggestions would be gratefully received.

Thanks


VBA Code:
Dim ColumnOrder As Variant, ndx As Integer
Dim Found As Range, counter As Integer
    ColumnOrder = Array("Custom Case ID", "Referral Date", "Last Name", "First Name", "Zip Code", "Locator Info")
counter = 1
  
For ndx = LBound(ColumnOrder) To UBound(ColumnOrder)
    Set Found = Rows("1:1").Find(ColumnOrder(ndx), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
    If Not Found Is Nothing Then
        If Found.Column <> counter Then
            Found.EntireColumn.Cut
            Columns(counter).Insert Shift:=xlToRight
            Application.CutCopyMode = False
        End If
    counter = counter + 1
    End If
Next ndx
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
What is the value of counter when you get the error?
 
Upvote 0
Hi Joe - For some reason, I haven't been able to get it to not work for a bit, but when it doesn't work, it seems to be on the ZIP code column, so I guess that would be count = 5 (?)
 
Upvote 0
Hi Joe - For some reason, I haven't been able to get it to not work for a bit, but when it doesn't work, it seems to be on the ZIP code column, so I guess that would be count = 5 (?)
If Zip Code happens to be in the very last column (col number 16,384), the code will encounter an error at this line:
Columns(counter).Insert Shift:=xlToRight
because you can't shift a column off the sheet as shift:= xlToRight demands.

Is it possible that occasionally Zip Code might be in the last column (column letters XFD)?
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,530
Members
448,969
Latest member
mirek8991

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