+ Reply to Thread
Results 1 to 3 of 3

Macro to format data from one column to multiple columns

Hybrid View

  1. #1
    Registered User
    Join Date
    09-30-2013
    Location
    'merica
    MS-Off Ver
    Excel 2003
    Posts
    2

    Macro to format data from one column to multiple columns

    Originally I was manually entering data from a website into excel. It was time consuming and frustrating. Then I discovered Excel's web query tool. I used the web query tool and was able to scrape all the data into column B. It now looks something like this:

    Event Name
    Date
    Name(s)
    Number

    REPEAT

    I was able to use a macro I found on microsoft support to remove other superfluous data that came in with the scrape and then delete the empty row and shift up. I only need the event name, the date, and the Name(s) from that data. I was not able to adjust the macro to remove the cells with random numbers though.
    Sub DeleteCells3()
    
        Dim rng As Range, i As Integer
    
        'Set the range to evaluate to rng.
        Set rng = Range("A1:A10")
    
        'Loop backwards through the rows
        'in the range that you want to evaluate.
        For i = rng.Rows.Count To 1 Step -1
    
            'If cell i in the range contains an "x", delete the entire row.
            If rng.Cells(i).Value = "x" Then rng.Cells(i).EntireRow.Delete
        Next
    
    End Sub
    What I'd like is for the table to look like this:
    Date Event Name Name(s)
    Date Event Name Name(s)

    Can anyone help me with creating a macro that will go though and format the table the way I have shown? Thanks for any advice!

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Macro to format data from one column to multiple columns

    Ok for fun.

    - all data is in column B and starts at row 1
    - a data block consists of four rows
    - row 4 (Number) is deleted, row 3 (names) is moved to column C on event row, and row 2 (Date) is moved to column A on event row

    If yes, try

    Public Sub FormatWebData()
    
    '#
    '# declare private variables
    '#
       Dim pvt_lng_RowNumber As Long
       
    '#
    '# loop through all rows on the worksheet in reverse order - the parent rows
    '# are considered to be at row 1, 5, 9 etc - the data blocks consist of four rows - the
    '# row number within the group determines the action
    '#
       With ThisWorkbook.Worksheets("Sheet1")
          For pvt_lng_RowNumber = .Cells(.Rows.Count, "B").End(xlUp).Row To 1 Step -1
          
             Select Case pvt_lng_RowNumber Mod 4
                Case 3
                   .Cells(pvt_lng_RowNumber - 2, "C").Value = .Cells(pvt_lng_RowNumber, "B").Value
                   .Rows(pvt_lng_RowNumber).Delete
                Case 2
                   .Cells(pvt_lng_RowNumber - 1, "A").Value = .Cells(pvt_lng_RowNumber, "B").Value
                   .Rows(pvt_lng_RowNumber).Delete
                Case 0
                   .Rows(pvt_lng_RowNumber).Delete
             End Select
          Next pvt_lng_RowNumber
       End With
    
    End Sub
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    09-30-2013
    Location
    'merica
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Macro to format data from one column to multiple columns

    Thank you! Exactly what I needed!!! Marked as solved and repped you. I really can't say thank you enough you have just saved me an enormous amount of time.

+ 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. [SOLVED] Macro to Sum Data in Multiple Columns based off Date in Column A
    By bdt99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2013, 06:13 PM
  2. Need a macro to format data and delete columns and create new column
    By aacod in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-12-2013, 12:15 PM
  3. Need a macro to format data and delete columns and create new column
    By aacod in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-12-2013, 12:14 PM
  4. Replies: 1
    Last Post: 11-05-2012, 12:52 AM
  5. macro for sorting data from one column to multiple columns
    By hck in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-07-2012, 08:44 AM

Tags for this Thread

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