Switch over Macro - Help

Olearus

New Member
Joined
Feb 10, 2011
Messages
11
Dear friends,

I have adata base. In this data base the main comluns are hhcode, village, ward andproperty. The property column can take tree values (1.00, 2.00 and 3.00) and I havethree different necessities depending on the case:<o:p></o:p>
<o:p> </o:p>
1) Whenhhcode, village and ward are the same between two rows (for example in row 4and 5 these columns take the following values: hhcode=1.00, village= 12, ward=3) and property has the 2.00 value in the row 5, I will need that all the row 5up to row 4 following the data which are contented in this row, so from Mcolumn<o:p></o:p>
2) When hhcode, village and ward are the samebetween three rows (for example in row 6, 7 and 8 columns take the followingvalues: hhcode=1.00, village= 7, ward= 6) and property takes the 2.00 value inthe row 7 and the 3.00 value in row 8, I will need that all the data in the row7 up to row 6 following the data which are in this row, so from M column and allthe data in the row 8 up to row 6 following the data which are in this row sofrom Y column<o:p></o:p>
3) When hhcode, village and ward are the samebetween two rows (for example in row 9 and 10 columns take the followingvalues: hhcode=1.00, village= 8, ward= 5) and property takes the 3.00 value inthe row 10, I will need that all the row 10 up to row 9 from Y column

Thank you very much for your help.

Olearus
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1hhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultivhhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultivhhcodevillage
200011131.0030.0050.0012.0056.0056.000.000.000.0000011132.000.000.000.000.000.00######0.00
3
400011221.0032.0024.009.000.000.0090.0090.000.00
500011222.000.000.000.000.000.00#########0.00
60001761.0010.005.000.000.000.0018.0018.000.00
70001762.000.000.000.000.000.0024.0024.000.00
80001763.000.000.000.000.000.00#########0.00
90001851.0018.000.004.000.000.000.000.000.00
100001853.000.000.000.000.0020.0054.0054.000.00
110001941.0018.000.000.000.000.0040.0040.000.00
120002191.008.0075.000.000.000.00#########0.00
1300021131.0030.005.0018.000.000.00#########0.00
1400021133.000.000.000.000.000.00#########0.00
1500021221.0032.0024.009.000.000.0075.0075.000.00
1600021222.000.000.000.000.000.0060.0060.000.00
1700021223.000.000.000.000.000.0075.0075.000.00
1800021321.0021.000.000.000.000.000.000.000.00
1900021322.000.000.000.000.000.0048.0048.000.00

<colgroup><col style="width: 60pt;" width="80"> <col style="width: 35pt; mso-width-source: userset; mso-width-alt: 1718;" width="47"> <col style="width: 31pt; mso-width-source: userset; mso-width-alt: 1499;" width="41"> <col style="width: 28pt; mso-width-source: userset; mso-width-alt: 1353;" width="37"> <col style="width: 42pt; mso-width-source: userset; mso-width-alt: 2048;" width="56"> <col style="width: 30pt; mso-width-source: userset; mso-width-alt: 1462;" width="40"> <col style="width: 29pt; mso-width-source: userset; mso-width-alt: 1426;" width="39"> <col style="width: 37pt; mso-width-source: userset; mso-width-alt: 1792;" width="49"> <col style="width: 28pt; mso-width-source: userset; mso-width-alt: 1353;" width="37"> <col style="width: 32pt; mso-width-source: userset; mso-width-alt: 1536;" width="42"> <col style="width: 31pt; mso-width-source: userset; mso-width-alt: 1499;" width="41"> <col style="width: 29pt; mso-width-source: userset; mso-width-alt: 1426;" width="39"> <col style="width: 27pt; mso-width-source: userset; mso-width-alt: 1316;" width="36"> <col style="width: 35pt; mso-width-source: userset; mso-width-alt: 1682;" width="46"> <col style="width: 31pt; mso-width-source: userset; mso-width-alt: 1499;" width="41"> <col style="width: 23pt; mso-width-source: userset; mso-width-alt: 1133;" width="31"> <col style="width: 39pt; mso-width-source: userset; mso-width-alt: 1901;" width="52"> <col style="width: 24pt; mso-width-source: userset; mso-width-alt: 1170;" span="8" width="32"> <col style="width: 38pt; mso-width-source: userset; mso-width-alt: 1865;" width="51"> <col style="width: 48pt; mso-width-source: userset; mso-width-alt: 2340;" width="64"> <tbody>
</tbody>
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Olearus,


Sample raw data in worksheet Sheet1:


Excel Workbook
ABCDEFGHIJKL
1hhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultiv
2111313050125656000
31113200000######0
411221322490090900
51122200000#########0
6176110500018180
717620000024240
8176300000#########0
91851180400000
10185300002054540
11194118000040400
122191875000#########0
13211313051800#########0
142113300000#########0
1521221322490075750
16212220000060600
17212230000075750
1821321210000000
19213220000048480
20
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
ABCDEFGHIJKLMNOPQ
1hhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultivhhcodevillagewardPropertyHome
2111313050125656000111320
311221322490090900112220
417611050001818017620
5185118040000018530
6194118000040400
72191875000#########0
8211313051800#########0211330
921221322490075750212220
1021321210000000213220
11
Results




1. What is the worksheet name for the raw data?

2. What is the worksheet name for the results?

3. Is the raw data already sorted/grouped by columns A, B, C?
 
Upvote 0
Olearus,


Sample raw data in worksheet Sheet1:


Excel Workbook
ABCDEFGHIJKL
1hhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultiv
2111313050125656000
31113200000######0
411221322490090900
51122200000#########0
6176110500018180
717620000024240
8176300000#########0
91851180400000
10185300002054540
11194118000040400
122191875000#########0
13211313051800#########0
142113300000#########0
1521221322490075750
16212220000060600
17212230000075750
1821321210000000
19213220000048480
20
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
ABCDEFGHIJKL
1hhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultiv
2111313050125656000
311221322490090900
4176110500018180
51851180400000
6194118000040400
72191875000#########0
8211313051800#########0
921221322490075750
1021321210000000
11
Results





Excel Workbook
MNOPQRSTUVWX
1hhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultiv
21113200000######0
31122200000#########0
417620000024240
5185300002054540
6
7
82113300000#########0
9212220000060600
10213220000048480
11
Results





Excel Workbook
YZAAABACADAEAFAGAHAIAJ
1hhcodevillagewardPropertyHomePondGardenCultivOneCultivTwoCultivThrCultivIrriUncultiv
2
3
4176300000#########0
5
6
7
8
9212230000075750
10
11
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, 02/04/2013
' http://www.mrexcel.com/forum/excel-questions/683313-switch-over-macro-help.html
Dim w1 As Worksheet, wR As Worksheet
Dim r As Long, rr As Long, lr As Long, lr2 As Long, nr As Long, c As Long, nc As Long, n As Long, i As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
lr = w1.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
w1.Range("M1") = "M"
With w1.Range("M2:M" & lr)
  .FormulaR1C1 = "=RC[-12]&RC[-11]&RC[-10]"
  .Value = .Value
End With
w1.Columns(13).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=w1.Columns(14), Unique:=True
lr2 = w1.Cells(Rows.Count, 14).End(xlUp).Row
With w1.Range("O2:O" & lr2)
  .Formula = "=COUNTIF($M:$M,$N2)"
  .Value = .Value
End With
n = Application.Max(w1.Range("O2:O" & lr2))
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
c = 1
For i = 1 To n
  wR.Cells(1, c).Resize(, 12).Value = w1.Cells(1, 1).Resize(, 12).Value
  c = c + 12
Next i
For r = 2 To lr
  n = Application.CountIf(w1.Columns(13), w1.Cells(r, 13).Value)
  nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
  nc = 1
  rr = r
  For i = 1 To n Step 1
    w1.Cells(rr, 1).Resize(, 12).Copy wR.Cells(nr, nc)
    rr = rr + 1
    nc = nc + 12
  Next i
  r = r + n - 1
Next r
w1.Range("M1:O" & lr).ClearContents
wR.Cells.EntireColumn.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
Olearus,

I forgot to say, that the macro will work correctly only if your data is grouped/sorted by your example in columns A, B, and C.
 
Upvote 0

Forum statistics

Threads
1,215,488
Messages
6,125,092
Members
449,206
Latest member
ralemanygarcia

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