VBA To Move Column Based On Value In Row 1

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
235
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
 

Some videos you may like

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.

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
235
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
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
45,204
Office Version
365
Platform
Windows
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
 

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
235
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
 

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
235
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:

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
45,204
Office Version
365
Platform
Windows
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?
 

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
235
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?
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
45,204
Office Version
365
Platform
Windows
OK, no problem. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,102,369
Messages
5,486,449
Members
407,547
Latest member
Sankarasrinivas

This Week's Hot Topics

Top