Restructuring Data

davesfx

New Member
Joined
Aug 10, 2011
Messages
5
I am needing some help restructuring some data.

I need to consolidate such as shown below. Results can be placed into another sheet. The zeros can be shown if need be and I can test and delete afterwards.

Thank you and Kindest Regards,

Davesfx

Excel Workbook
ABCD
1DATESChicago,ILLos Angeles,CALas Vegas,NV
210-Aug000
311-Aug150
412-Aug201
513-Aug001
Sheet1
Excel Workbook
ABC
11DATESDistrictCount
1211-AugChicago,IL1
1311-AugLos Angeles,CA5
1412-AugChicago,IL2
1512-AugLas Vegas,NV1
1613-AugLas Vegas,NV1
Sheet1
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I had visited the page before but wasn't interested in setting up a pivot table. Had I taken the time to read the whole page, I would have seen the VBA option at the bottom.

thank you very much for your time! Greatly appreciated.

Now..back to coding!
 
Upvote 0
OK, but note that the Pivot Table is a temporary arrangement... once you have the end result, it's actually a data sheet and you can delete the Pivot Table if you wish.
 
Upvote 0
davesfx,


Thank you for the Private Message.


The macro will adjust for additional columns with titles in row 1 of worksheet Sheet1.


Sample raw data in worksheet Sheet1:


Excel Workbook
ABCD
1DATESChicago,ILLos Angeles,CALas Vegas,NV
210-Aug000
311-Aug150
412-Aug201
513-Aug001
6
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
ABC
1DATESDistrictCount
211-AugChicago,IL1
311-AugLos Angeles,CA5
412-AugChicago,IL2
512-AugLas Vegas,NV1
613-AugLas Vegas,NV1
7
Results





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 08/10/2011
' http://www.mrexcel.com/forum/showthread.php?t=570795
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a As Long, aa As Long, NR As Long, LC As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
wR.Range("A1:C1") = [{"DATES","District","Count"}]
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
LC = w1.Cells(1, Columns.Count).End(xlToLeft).Column
For a = 2 To LR Step 1
  For aa = 2 To LC Step 1
    If w1.Cells(a, aa) > 0 Then
      NR = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      wR.Range("A" & NR) = w1.Cells(a, 1)
      wR.Range("B" & NR) = w1.Cells(1, aa)
      wR.Range("C" & NR) = w1.Cells(a, aa)
    End If
  Next aa
Next a
wR.Range("A2:A" & NR).NumberFormat = "d-mmm"
wR.UsedRange.Columns.AutoFit
wR.Activate
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the ReorgData macro.
 
Upvote 0
Thank you very much.

I have been spinning my wheels for a couple of days. Both are lifesavers.

My day has gotten much better :)
 
Upvote 0
davesfx,

You are very welcome.

Glad I could help.

Thanks for the feedback.

Come back anytime.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top