seagreen_turtle
New Member
- Joined
- Sep 22, 2015
- Messages
- 2
I attempted to write some code to insert a blank row after each change of value in column B. I also change the fill color to grey, add the text "Totals:" to column C and right justify it. I still have one bug. It overwrites the header (column labels) to add the grey bar and totals. Can someone please help me with this? I only write VBA every once in a while so I can get stumped easily. Here's the code:
Before the code runs, the first row contains column headers. Please let me know if you need sample data. Thanks!
Code:
Sub InsertRowsAtValueChange()
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "Make sure value is $B:$B "
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Cells.Select
Selection.Columns.AutoFit
Application.ScreenUpdating = True
For i = WorkRng.Rows.Count To 2 Step -1
If WorkRng.Cells(i, 1).Value <> WorkRng.Cells(i - 1, 1).Value Then
WorkRng.Cells(i, 1).EntireRow.Insert
WorkRng.Cells(i, 1).EntireRow.Interior.Color = RGB(201, 196, 205)
WorkRng.Cells(i, 3).Value = "Totals: "
WorkRng.Cells(i, 3).HorizontalAlignment = xlRight
Application.Goto Reference:="Macro1"
End If
Next
End Sub
Before the code runs, the first row contains column headers. Please let me know if you need sample data. Thanks!