VBA To Move Column Based On Value In Row 1

TkdKidSnake

Board Regular
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
 

TkdKidSnake

Board Regular
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
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
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
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
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
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?
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top