Consulting

Results 1 to 7 of 7

Thread: Vba to move columns with certain headers

  1. #1

    Vba to move columns with certain headers

    Hi, Can you please help me with the following - I have an excel file that is being exported everyday, and the columns are either moved or more columns are added. I need a macro to arrange some of these columns in a specific order. For example: Initial columns order as per today: A B C D E F DSPID AssortID SKU SKUDescription Quantity ProjectName / Order always needed: A B C D E F DSPID ProjectName AssortID SKU SKUDescription Quantity. I have to "read" the headers of the columns and where it says "ProjectName", to move that whole column to B. Thank you very much! Iulia

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Please attach a sample workbook to overcome the following issues;

    Quote Originally Posted by iuliamihaela View Post
    I have an excel file that is being exported everyday, and the columns are either moved or more columns are added.
    Which columns are being moved? For example are any of the following columns being moved, DSPID AssortID SKU SKUDescription Quantity ProjectName / Order? Will they always be found within the first 6 columns?

    Initial columns order as per today: A B C D E F DSPID AssortID SKU SKUDescription Quantity ProjectName / Order
    Please don't mix column labels (Excel defined) and Column Headers (User defined) when trying to explain an issue.

    always needed: A B C D E F DSPID ProjectName AssortID SKU SKUDescription Quantity.
    Can I assume that the order of columns will always be the same, with the exception of ProjectName/Order which could be anywhere or should we be looking at making sure the order is always DSPID, ProjectName/Order, AssortID, SKU, SKUDescription, Quantity
    I have to "read" the headers of the columns and where it says "ProjectName", to move that whole column to B.
    Are we just moving the column ProjectName/Order to Column B?


    Can you see where I'm going with this? To attach a file, click on Go Advanced, scroll down to Manage Attachments and follow the prompts from there please. When responding think about the points I have raised here, and we'll be only too happy to have a look at your query.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Hi,

    I have attached a sample file example.xlsx - Sheet "Initial" contains the column headers in the order that is being exported, and Sheet "Final" contains the columns in the order I need. I have highlighted the columns that are being moved.

    I need the macro to rearrange the columns based on column header and not based on column labels, because the order in the file exported (Initial) is changing constantly and this would require updating the macro with the new column labels very often.


    Hope you can help.
    Thank you very much,
    Iulia

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Try the following on a sample worksheet:
    Private Sub Reorder_Columns()
    Dim arrColOrder As Variant, ndx As Integer
    Dim Found As Range, counter As Integer
    arrColOrder = Array("DSPID", "ProjectName", "ProjectStatus", "AssortID", "SKU", "SKUDescription", "Quantity")
    counter = 1
    Application.ScreenUpdating = False
    For ndx = LBound(arrColOrder) To UBound(arrColOrder)
    Set Found = Rows("1:1").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
    Application.ScreenUpdating = True
    End Sub
    Note Insert into the sheet module you wish to run it on.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Hi, I have tried the code and it does the following - it moves column W in my file (which is ProjectName) to column B. But the rest of the columns remain in the same order. Should I do anything else? I wanted to attach the file I used for testing, but it the Manage Attachments button doesn't work (and also I cannot format this paragraph in order to look normal, with spaces)...Therefore, please find the order of the columns below:
    DSPID AssortID SKU SKUDescription Quantity ProductVersion Language Platform Market ProductFormat OfferingType Config ActivationType PaymentType DistributionType EncodingType BOMTemplate Status AssortmentTargetEndDate AssortmentActualEndDate SKUCreationTargetEndDate SKUCreationActualEndDate ProjectName Project Status Retailer RetCountry Distributor DistCountry TechVendor TechCountry ECCID
    PM

    I am sorry for sending you the information like this. Hope you can help further. Thank you, Iulia

  6. #6
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    2
    Location
    Hi,
    I have a similiar issue and can't get my Macro to work...

    Right now I have a column called "057 - Interest Margin" as a header (row 1) in column AM. I want VBA to search for that value and then cut the entire column and move it to column S. If it can't find the column "057 - Interest Margin" VBA is supposed to insert a new column (column S) called "057 - Interest Margin".

    Thanks
    Stefan

  7. #7
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    2
    Location
    Quote Originally Posted by bayern21 View Post
    Hi,
    I have a similiar issue and can't get my Macro to work...

    Right now I have a column called "057 - Interest Margin" as a header (row 1) in column AM. I want VBA to search for that value and then cut the entire column and move it to column S. If it can't find the column "057 - Interest Margin" VBA is supposed to insert a new column (column S) called "057 - Interest Margin".

    Thanks
    Stefan
    so what I have so far is the following:

    For Each rng In Range("A1:BZ1")
    If rng.Value = "057 - Interest Margin" Then
    rng.EntireColumn.Cut
    Sheets("Sheet1").Select
    Columns("S:S").Select
    Selection.Insert Shift:=xlToRight
    End If
    Next rng

    What I am missing right now is that a new column and a specific header is added in case "057 - Interest Margin" cannot be found.

    I came up with this but it screws up everything because it inserts the column 18 times????


    For Each Rng In Range("A1:BZ1")
    If Rng.Value = "057 - Interest Margin" Then
    Rng.EntireColumn.Cut
    Sheets("Sheet1").Select
    Columns("S:S").Select
    Selection.Insert Shift:=xlToRight
    Else:
    Columns("S:S").Select
    Selection.Insert Shift:=xlToRight
    Range("S1").Select
    ActiveCell.FormulaR1C1 = "057 - Interest Margin"
    Range("S2").Select
    End If
    Next Rng
    Last edited by bayern21; 11-15-2016 at 03:23 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •