+ Reply to Thread
Results 1 to 2 of 2

Move unknown number of headings into cells in new columns

Hybrid View

  1. #1
    Registered User
    Join Date
    06-03-2015
    Location
    Odense, Denmark
    MS-Off Ver
    2010
    Posts
    1

    Move unknown number of headings into cells in new columns

    Hi,

    I have a sheet with a list of transactions. The headings for each account is listed above the transactions. In order to create a list, I need to move each heading to its own new column on each transaction line. These could be the headings:

    Account
    S3
    S4
    G1
    G2


    They are following by a number of transactions, each in a separate row. All transaction rows need to contain the headings for that specific account.

    I have figured out how to make it work when there are 5 headings, however, if my colleagues should also benefit from this macro, it needs to be changed so that there can be between 5 and 8 headings.

    I have also figured out how to create the right number of new columns, but I struggle with copying the headings to the cells in the new columns. This is the way I have done it when knowing the number of headings:

    For w = 1 To Cells.SpecialCells(xlLastCell).Row Step 1
    
        'Find first row in column F with the value Account
        Set soeg = Columns("F").Find(What:="Account", After:=Range("F1"), LookIn:=xlFormulas _ 
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
        If Not soeg Is Nothing Then
        
        soeg.Activate
        konto = ActiveCell.Row 'Row number containing the account heading
        s3 = konto + 1 'Row number containing the S3 heading
        s4 = konto + 2 'Row number containing the S4 heading
        g1 = konto + 3 'Row number containing the G1 heading
        g2 = konto + 4 'Row number containing the G2 heading
        postering = konto + 5 'Row number of the first transaction
        Columns("F").Find(What:="", After:=Cells(postering, 6), LookIn:=xlFormulas _
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        tom_celle = ActiveCell.Row 'Row number of empty row following the last transaction
        sidste_postering = tom_celle - 1 'Row number of last transaction
            
        start = postering
            
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
        
        For p = start To sidste_postering Step 1
        
        'For each transaction line copy the right heading to the right cell in the new column
        WS.Cells(postering, 1).Value = ActiveSheet.Cells(konto, 7).Value
        WS.Cells(postering, 2).Value = ActiveSheet.Cells(s3, 7).Value
        WS.Cells(postering, 3).Value = ActiveSheet.Cells(s4, 7).Value
        WS.Cells(postering, 4).Value = ActiveSheet.Cells(g1, 7).Value
        WS.Cells(postering, 5).Value = ActiveSheet.Cells(g2, 7).Value
        postering = postering + 1
        
        Next p
    
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        
        End If
    
    Next w
    How do I do this when I do not know the number of headings?

    Best regards,
    LKottal

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Move unknown number of headings into cells in new columns

    Hi LKottal,

    Welcome to the Forum!

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sum 12 Rows Unknown Number Of Columns
    By goss in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-28-2012, 08:34 AM
  2. Add a preset number of cells after unknown number of leading zeroes
    By Marweas in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-24-2012, 09:23 PM
  3. Replies: 3
    Last Post: 02-13-2012, 04:27 PM
  4. Sort unknown number of columns by row 1
    By rwqiii in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-27-2011, 04:42 PM
  5. Macro required to move Columns and addidng columns with headings
    By sponge_designs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-03-2009, 08:20 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1