VBA: Rearrange columns

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone,

Basically Im trying to rearrange my columns based on the array "Row Labels", "Bereavement", "Provincial Leave", "Medical Waiver". The error happens when it finds Medical Waiver and then cuts.

Not sure how to fix this or if its even the right method.

Row LabelsBereavementMedical WaiverProvincial Leave
1234Not UsedNot Used7
1234Not UsedNot Used9
1234Not UsedNot Used9
1234Not UsedNot Used7

*xl2bb crashes when I capture using Mini Sheet. Row Label is A1*

VBA Code:
Sub reordercolumns()
    Dim arrColOrder As Variant, ndx As Integer
    Dim Found As Range, Counter As Integer, fc As Range

        arrColOrder = Array("Row Labels", "Bereavement", "Provincial Leave", "Medical Waiver")
       
        Counter = 1
          
        For ndx = LBound(arrColOrder) To UBound(arrColOrder)
       
            Set Found = Range("A1:D1").Find(arrColOrder(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

End Sub

Thanks for any help.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
@gd6noob
Try this:
In the process, the code will change the header by adding sequential number (i.e. "000Row Labels", "001Bereavement",..etc), then sort data Left to Right, then change the header back to their original value.
If you don't like the idea to change the header in the process (in case something goes wrong) then we could use a helper row below the data.

VBA Code:
Sub gd6noob()
Dim arrColOrder As Variant, ndx As Integer
Dim Found As Range, Counter As Integer, fc As Range
Dim c As Range, i As Long, g As Range

arrColOrder = Array("Row Labels", "Bereavement", "Provincial Leave", "Medical Waiver")

Set c = Range("A1:D1") 'header

'adding sequential number to each header
For Each x In arrColOrder
res = Application.Match(x, c, 0)
    c.Cells(res) = Format(i, "000") & c.Cells(res)
    i = i + 1
Next

With ActiveSheet.Sort
   .SortFields.Clear
     .SortFields.Add Key:=c, Order:=xlAscending
     .SetRange c.EntireColumn
     .Header = xlNo
     .Orientation = xlLeftToRight
     .Apply
End With

'change the header back to original value
For Each g In c
    g = Mid(g, 4, 1000)
Next

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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