Rearrange of columns

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this code and nost sure why its not rearragne the columns, the problem is it not arraning the IP Address column not sure why.

Any reason, as why it snot rearraing the columns are per the list in Ref1 sheet column E starting from 2

VBA Code:
Sub RearragneColumnsCopperADTRAN()

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
''''
''''  Rearrange Columns in "Copper ADTRAN" from Ref1 sheet column E
''''
''''
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

 
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Ref1")
    Set ws2 = Worksheets("Copper ADTRAN")

    ' Assuming you want to rearrange columns based on the order in Ref1 column E
    Dim orderRange As Range
    Set orderRange = ws1.Range("E2", ws1.Cells(Rows.Count, "E").End(xlUp))

    ' Create a dictionary to store the order of columns
    Dim columnOrder As Object
    Set columnOrder = CreateObject("Scripting.Dictionary")

    ' Populate the dictionary with column order
    Dim i As Integer
    For i = 1 To orderRange.Rows.Count
        columnOrder(orderRange.Cells(i, 1).Value) = i
    Next i

    ' Rearrange columns based on the order in the dictionary
    Dim ws2LastColumn As Integer
    ws2LastColumn = ws2.Cells(1, ws2.Columns.Count).End(xlToLeft).Column

    For i = 1 To ws2LastColumn
        Dim columnName As String
        columnName = ws2.Cells(1, i).Value

        If columnOrder.Exists(columnName) Then
            Dim targetIndex As Integer
            targetIndex = columnOrder(columnName)

            ' Check if the column needs to be moved
            If i <> targetIndex Then
                ws2.Columns(i).Cut
                ws2.Columns(targetIndex).Insert Shift:=xlToRight
            End If
        End If
    Next i
End Sub

Golden Automation Report V1.xlsm
ABCDEFGHIJK
1Item typePositionIP AddressNameNE Type/ReleaseSoftware revisionSerial numberHardware item numberHardware revisionFirmware revisionVendor
2NE10.74.76.57MMRSTRDEE_AMSLE-A_Switched-off-copper sunset TT#PR00025933944 / ADTRAN Copper
3Rack110.74.76.57 / ADTRAN Copper
4NE10.74.84.3MBGDYKHCH_CBABC-AhiX5635 / 5.6Hsaps.hix56xxsr56.04.002.cp.001ADTRAN Copper
5Rack110.74.84.3hiX5635 / 5.6HADTRAN Copper
6Shelf110.74.84.3G1100:2G:EhiX5635 / 5.6HLBADTN1427AA008 S50028-B3102-A1- 8not availableADTRAN Copper
7Module10110.74.84.3VPLU:72P2:EhiX5635 / 5.6Hshe_sr5.6_iu-vplu-b2_o.006LBADTN1435AJ065S50028-Q2062-B2-23v11_26_03_08 / bl 2.1.0ADTRAN Copper
8Module10210.74.84.3VPLU:72P2:EhiX5635 / 5.6Hshe_sr5.6_iu-vplu-b2_o.006LBADTN1435AJ348S50028-Q2062-B2-23v11_26_03_08 / bl 2.1.0ADTRAN Copper
9Module10310.74.84.3VPLU:72P2:EhiX5635 / 5.6Hshe_sr5.6_iu-vplu-b2_o.006LBADTN1435AJ440S50028-Q2062-B2-23v11_26_03_08 / bl 2.1.0ADTRAN Copper
10Module10410.74.84.3VPLU:72P2:EhiX5635 / 5.6Hshe_sr5.6_iu-vplu-b2_o.006LBADTN1437AR001S50028-Q2062-B2-23v11_26_03_08 / bl 2.1.0ADTRAN Copper
11Module10910.74.84.3CXUB21GE:4:EhiX5635 / 5.6Hshe_sr5.6_cxu-b2_o.228LBADTN1419AF023S50028-Q3120-B1-14cxu_xx-b.V09ADTRAN Copper
Copper ADTRAN




after running output
1701773298366.png


List is here column E sheet Ref1 starting from row no 2
1701773384491.png
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about trying Power Query. Assuming your table name is called Table1, you could do the following:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Reordered Columns" = Table.ReorderColumns(Source,{"Name", "NE Type/Release", "IP Address", "Software revision", "Vendor", "Item type", "Position", "Serial number", "Hardware item number", "Hardware revision", "Firmware revision"})
in
    #"Reordered Columns"
 
Upvote 0
no i am not use to power Query, vba is better for now
 
Upvote 0
Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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