VBA To Move Column Based On Value In Row 1

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am looking for some help, I am trying to create code that will move a column of data to another column location on the same worksheet based on the value found in row 1

On my report I have the following headers in row 1 plus many more however these are the important ones
Supplier (A)
Name (B)
Order (C)
Item Number (D)
Receipt Quantity (E)
Quantity Open (F)
Status (G)
Receipt Date (H)
Performance Date (I)
Due Date (J)
Need Date (K)

The issue I have is that these can appear anywhere on the extracted spreadsheet as there locations are user defined and I need several people to be able to produce the data required, next to each of the above there is a the letter for where I need these columns to be moved to. Once they have been moved everything after these columns can be deleted so if this could be part of the above it would be appreciated.

Obviously if anyone can provide the code for one of these and then I only need to replicate the detail for the others only change the search criteria and end location.

Many thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I've only gone and done it, well the sort anyway - see below

Code:
Sub columnOrder()Dim search As Range
Dim cnt As Integer
Dim colOrdr As Variant
Dim indx As Integer


Application.ScreenUpdating = False


colOrdr = Array("Supplier", "Name", "Order", "Item Number", "Receipt Quantity", "Quantity Open", "Status", "Receipt Date", "Performance Date", "Due Date", "Need Date")


cnt = 1




For indx = LBound(colOrdr) To UBound(colOrdr)
    Set search = Rows("1:1").Find(colOrdr(indx), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
    If Not search Is Nothing Then
        If search.Column <> cnt Then
            search.EntireColumn.Cut
            Columns(cnt).Insert Shift:=xlToRight
            Application.CutCopyMode = False
        End If
    cnt = cnt + 1
    End If
Next indx


Application.ScreenUpdating = True


End Sub
Hi all,

I am looking for some help, I am trying to create code that will move a column of data to another column location on the same worksheet based on the value found in row 1

On my report I have the following headers in row 1 plus many more however these are the important ones
Supplier (A)
Name (B)
Order (C)
Item Number (D)
Receipt Quantity (E)
Quantity Open (F)
Status (G)
Receipt Date (H)
Performance Date (I)
Due Date (J)
Need Date (K)

The issue I have is that these can appear anywhere on the extracted spreadsheet as there locations are user defined and I need several people to be able to produce the data required, next to each of the above there is a the letter for where I need these columns to be moved to. Once they have been moved everything after these columns can be deleted so if this could be part of the above it would be appreciated.

Obviously if anyone can provide the code for one of these and then I only need to replicate the detail for the others only change the search criteria and end location.

Many thanks
 
Upvote 0
I think my code is pretty close to yours anyway, but here is what I was developing. :)
Test in a copy of your workbook.

Code:
Sub Rearrange_Columns()
  Dim myCols As Variant
  Dim i As Long, c As Long, k As Long
  
  myCols = Split("Supplier,Name,Order,Item Number,Receipt Quantity,Quantity Open,Status,Receipt Date,Performance Date,Due Date,Need Date", ",")
  Application.ScreenUpdating = False
  For i = UBound(myCols) To 0 Step -1
    c = 0
    On Error Resume Next
    c = Rows(1).Find(What:=myCols(i), LookAt:=xlWhole, MatchCase:=False).Column
    On Error GoTo 0
    If c > 0 Then
      Columns(c).Cut
      Columns(1).Insert
      k = k + 1
    End If
  Next i
  ActiveSheet.UsedRange.Offset(, k).EntireColumn.Delete
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Yours also works, I think I'll use this as seems to be a little quicker - thank you.

I think my code is pretty close to yours anyway, but here is what I was developing. :)
Test in a copy of your workbook.

Code:
Sub Rearrange_Columns()
  Dim myCols As Variant
  Dim i As Long, c As Long, k As Long
  
  myCols = Split("Supplier,Name,Order,Item Number,Receipt Quantity,Quantity Open,Status,Receipt Date,Performance Date,Due Date,Need Date", ",")
  Application.ScreenUpdating = False
  For i = UBound(myCols) To 0 Step -1
    c = 0
    On Error Resume Next
    c = Rows(1).Find(What:=myCols(i), LookAt:=xlWhole, MatchCase:=False).Column
    On Error GoTo 0
    If c > 0 Then
      Columns(c).Cut
      Columns(1).Insert
      k = k + 1
    End If
  Next i
  ActiveSheet.UsedRange.Offset(, k).EntireColumn.Delete
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Sorted the delete function as well

Code:
Dim luc As Long
With ActiveSheet
luc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
If luc > 11 Then
    .Columns(12).Resize(, luc - 12 + 1).Delete
End If
End With

Hi all,

I am looking for some help, I am trying to create code that will move a column of data to another column location on the same worksheet based on the value found in row 1

On my report I have the following headers in row 1 plus many more however these are the important ones
Supplier (A)
Name (B)
Order (C)
Item Number (D)
Receipt Quantity (E)
Quantity Open (F)
Status (G)
Receipt Date (H)
Performance Date (I)
Due Date (J)
Need Date (K)

The issue I have is that these can appear anywhere on the extracted spreadsheet as there locations are user defined and I need several people to be able to produce the data required, next to each of the above there is a the letter for where I need these columns to be moved to. Once they have been moved everything after these columns can be deleted so if this could be part of the above it would be appreciated.

Obviously if anyone can provide the code for one of these and then I only need to replicate the detail for the others only change the search criteria and end location.

Many thanks
 
Last edited:
Upvote 0
Sorted the delete function as well

Code:
Dim luc As Long
With ActiveSheet
luc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
If luc > 11 Then
    .Columns(12).Resize(, luc - 12 + 1).Delete
End If
End With
Mine already had the delete in it. Didn't it do what you wanted?
 
Upvote 0
Yes but I had already sorted the code to delete so if someone wanted just this thought it would be worthwhile adding it.

Thank you for your help.

Mine already had the delete in it. Didn't it do what you wanted?
 
Upvote 0

Forum statistics

Threads
1,214,428
Messages
6,119,420
Members
448,895
Latest member
omarahmed1

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