Excel: Rearrange data in an export of raw data


Question:  I have an Excel spreadsheet that contains an export of some raw data. However, the data needs to be rearranged as follows:

If cell M? displays "Contract Information" then copy and paste cells O-S into cell M of the same row
If cell K? displays "Location" then cut and paste cells K-AD into cell T of the same row. Then copy and paste the previous row's cells K-S down to K into this row
Copy down the category name in Column L replacing "Loads"


Answer:  Let's take a look at an example.


Download Excel spreadsheet (as demonstrated below)




In this spreadsheet, we've created a macro called RearrangeData. You can run the macro by selecting Macro > Macros under the Tools menu. Then highlighting the macro called RearrangeData and clicking on the Run button.


Once the macro has run, the spreadsheet will look as follows:




You can press Alt-F11 to view the VBA code.


Macro Code:

The macro code looks like this:

Sub RearrangeData()


    Dim LRow As Integer
    Dim LCategory As String


    LRow = 1
    LCategory = ""

    'Move through records until an empty cell is found in column A
    While IsEmpty(Range("A" & CStr(LRow)).Value) = False


        'If cell M? displays "Contract Information" then copy and paste
        'cells O-S into cell M of the same row
        If Range("M" & CStr(LRow)).Value = "Contract Information" Then
            Range("O" & LRow & ":S" & LRow).Select
            Selection.Copy
            Range("M" & LRow).Select
            ActiveSheet.Paste
        End If


        'If cell K? displays "Location" then cut and paste cells K-AD into
        'cell T of the same row. Then copy and paste the previous row's
        'cells K-S down to K into this row
        If Range("K" & CStr(LRow)).Value = "Location" Then
            'Cut and paste cells K-AD into cell T of the same row
            Range("K" & LRow & ":AD" & LRow).Select
            Selection.Cut
            Range("T" & LRow).Select
            ActiveSheet.Paste


            'Copy and paste the previous row's cells K-S down to K
            'into this row
            Range("K" & LRow - 1 & ":S" & LRow - 1).Select
            Selection.Copy
            Range("K" & LRow).Select
            ActiveSheet.Paste


        End If


        'Copy down the category name in Column L replacing "Loads"
        If Range("L" & CStr(LRow)).Value = "Loads" Then
            Range("L" & CStr(LRow)).Value = LCategory
        'Next category name
        Else
            LCategory = Range("L" & CStr(LRow)).Value
        End If


        LRow = LRow + 1
    Wend


End Sub

Spreadsheet News


Book Shop


Visit our busy book shop, were you can find latest books for beginners as well as advanced excel users.



Software Library




Spreadsheet123

Spreadsheet 123 Spreadsheet Software Development

Home | About Us | Our Services | Software | Spreadsheets Library | Excel Tutorials | Useful Links | Site Map | Contact Us
Home
About Us
About Spreadsheet
Book Shop
Excel Tutorials
1
Spreadsheets Library
3
4
5
6
Our Services
8
Site Map
Software
11
12
13
14
15
16
17