Solved

Use VBS to rearrange Excel spreadsheet

Posted on 2011-02-25
4
205 Views
Last Modified: 2012-05-11
I would like to know the VBA to rearrange a spreadsheet.

This is how the spreadsheet is laid out
Part_ID      1/1/2011  1/15/2011  2/1/2011      3/15/2011  4/1/2011
ABC                           2                30                       2                  2
X12HH               1             2                  2                       9                  6
BNH212        2                              79                     80      

I want to change to this
Part_ID      Want_Date      QTY
ABC      1/1/2011              0
ABC      1/15/2011      2
ABC      2/1/2011             30
ABC      3/15/2011      2
ABC      4/1/2011        2
X12HH      1/1/2011              1
X12HH      1/15/2011      2
X12HH      2/1/2011        2
X12HH      3/15/2011      9
X12HH      4/1/2011              6
BNH212   1/1/2011      2
BNH212        1/15/2011      0
BNH212       2/1/2011     79
BNH212       3/15/2011    80
BNH212       4/1/2011        0

Attached spreadsheet shows how I want the data to look.

Data-Example.xls
0
Comment
Question by:tprocket
  • 2
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34984227
Assuming that your source data is on its own worksheet, with headings in Row 1:



Sub Normalize()
    
    Dim r As Long, c As Long, LastR As Long, LastC As Long, arr As Variant, DestR As Long
    
    With ActiveSheet
        LastR = .Cells(.Rows.Count, 1).End(xlUp).Row
        LastC = .Cells(1, .Columns.Count).End(xlToLeft).Column
        arr = .Range(.[a1], .Cells(LastR, LastC)).Value
    End With
    
    Worksheets.Add
    
    [a1:c1] = Array("Part_ID", "Want_Date", "Qty")
    DestR = 1
    
    For r = 2 To LastR
        For c = 2 To LastC
            DestR = DestR + 1
            Range(Cells(DestR, 1), Cells(DestR, 3)) = Array(arr(r, 1), arr(1, c), arr(r, c))
        Next
    Next
    
    Columns.AutoFit
    
    MsgBox "Done"
    
End Sub

Open in new window

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 34984478
LOL...I've even used formulas to do this, but it gets a bit complicated...using things like INDIRECT() combined with ROW() and COLUMN().
If you'd like to see that, let me know.
0
 

Author Comment

by:tprocket
ID: 34984561
Thanks that works out great!

I have a couple of more questions

1. What if I want to add more columns?
I modified this section to include a new column    

It was
    [a1:c1] = Array("Part_ID", "Want_Date", "Qty")

I changed it to
[a1:d1] = Array("Part_ID", "Desc", "Want_Date", "Qty")

but it did not add it to columns correctly, can you let me know what else I need to change

2. Also can I create a worksheet called Normalized and replace the data with the new data

0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 34984719
Assuming that "Desc" comes in the second column of the source data, and the "dates" are now in the 3rd through Nth columns...



Sub Normalize()
    
    Dim r As Long, c As Long, LastR As Long, LastC As Long, arr As Variant, DestR As Long
    
    With ActiveSheet
        LastR = .Cells(.Rows.Count, 1).End(xlUp).Row
        LastC = .Cells(1, .Columns.Count).End(xlToLeft).Column
        arr = .Range(.[a1], .Cells(LastR, LastC)).Value
    End With
    
    Worksheets.Add
    
    [a1:d1] = Array("Part_ID", "Desc", "Want_Date", "Qty")
    DestR = 1
    
    For r = 2 To LastR
        For c = 3 To LastC
            DestR = DestR + 1
            Range(Cells(DestR, 1), Cells(DestR, 4)) = Array(arr(r, 1), arr(r, 2), arr(1, c), arr(r, c))
        Next
    Next
    
    Columns.AutoFit
    
    MsgBox "Done"
    
End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now