Insetr Text and SubTotal/Autosum

Zibi

Board Regular
Joined
Feb 2, 2012
Messages
73
Hi,

Can Someone help me with the VBA? I have code but i dont know how to change to insert Text and then Total.



Sub Test()
Dim Rng As Range
Dim x As Long


Set Rng = Range("F1:F" & Range("F65536").End(xlUp).Row)
For x = Rng.Rows.Count To 2 Step -1
If Rng.Cells(x, 1).Offset(-1, 0).Value <> Rng.Cells(x, 1).Value Then

'insert Text "TOTAL"


Rng.Cells(x, 1).EntireRow.Insert Shift:=xlDown


'insert AutoSUM formula next to "TOTAL"

Rng.Cells(x, 1).EntireRow.Insert Shift:=xlDown
Rng.Cells(x, 1).EntireRow.Insert Shift:=xlDown

End If
Next x
End Sub





Apple3
Apple
Apple4
Apple
Apple
Total7
Orange
Orange3
Orange
Orange
Total3
Cheese9
Cheese
Cheese3
Total12

<TBODY>
</TBODY>
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Zibi,

Your text display shows inserting three rows?

Sample raw data:


Excel 2007
FG
1Apple3
2Apple
3Apple4
4Apple
5Apple
6Orange
7Orange3
8Orange
9Orange
10Cheese9
11Cheese
12Cheese3
13
14
15
16
17
18
19
20
Sheet1


After the macro:


Excel 2007
FG
1Apple3
2Apple
3Apple4
4Apple
5Apple
6Total7
7
8
9Orange
10Orange3
11Orange
12Orange
13Total3
14
15
16Cheese9
17Cheese
18Cheese3
19Total12
20
Sheet1


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).

Code:
Sub ReorgData()
' hiker95 05/29/2014, ME780943
Dim r As Long, lr As Long
Dim Area As Range, sr As Long, er As Long
lr = Cells(Rows.Count, 6).End(xlUp).Row
For r = lr To 2 Step -1
  If Cells(r - 1, 6) <> Cells(r, 6) Then
    Rows(r).Resize(3).Insert
  End If
Next r
For Each Area In Range("F1", Range("F" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    With Cells(er + 1, 6)
      .Value = "Total"
      .Font.Bold = True
    End With
    Cells(.Row + .Rows.Count, 7).Value = Evaluate("=Sum(G" & .Row & ":G" & .Row + .Rows.Count - 1 & ")")
  End With
Next Area
Columns("F:G").AutoFit
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
Zibi,

Here is a version of the macro that will insert two rows.

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).

Code:
Sub ReorgDataV2()
' hiker95 05/29/2014, ME780943
Dim r As Long, lr As Long
Dim Area As Range, sr As Long, er As Long
lr = Cells(Rows.Count, 6).End(xlUp).Row
For r = lr To 2 Step -1
  If Cells(r - 1, 6) <> Cells(r, 6) Then
    Rows(r).Resize(2).Insert
  End If
Next r
For Each Area In Range("F1", Range("F" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    With Cells(er + 1, 6)
      .Value = "Total"
      .Font.Bold = True
    End With
    Cells(.Row + .Rows.Count, 7).Value = Evaluate("=Sum(G" & .Row & ":G" & .Row + .Rows.Count - 1 & ")")
  End With
Next Area
Columns("F:G").AutoFit
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 ReorgDataV2 macro.


Just let me know if you want to see the actual formulae in the correct cell?
 
Upvote 0
Zibi,

Here is a version that will insert the SUM formulae.

After the macro:


Excel 2007
FG
1Apple3
2Apple
3Apple4
4Apple
5Apple
6Total7
7
8Orange
9Orange3
10Orange
11Orange
12Total3
13
14Cheese9
15Cheese
16Cheese3
17Total12
18
Sheet1
Cell Formulas
RangeFormula
G6=SUM(G1:G5)
G12=SUM(G8:G11)
G17=SUM(G14:G16)


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).

Code:
Sub ReorgDataV3()
' hiker95 05/29/2014, ME780943
Dim r As Long, lr As Long
Dim Area As Range, sr As Long, er As Long
lr = Cells(Rows.Count, 6).End(xlUp).Row
For r = lr To 2 Step -1
  If Cells(r - 1, 6) <> Cells(r, 6) Then
    Rows(r).Resize(2).Insert
  End If
Next r
For Each Area In Range("F1", Range("F" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    With Cells(er + 1, 6)
      .Value = "Total"
      .Font.Bold = True
    End With
    Cells(.Row + .Rows.Count, 7).Formula = "=Sum(G" & .Row & ":G" & .Row + .Rows.Count - 1 & ")"
  End With
Next Area
Columns("F:G").AutoFit
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 ReorgDataV3 macro.
 
Upvote 0
Zibi,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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