How to avoid overwriting heading row in Excel after my VBA code runs

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:
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!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I don't see anything in the posted code that would cause the overwrite, unless it is in Macro1, which is called with the GoTo statement. but you didn't post that macro, so can't tell for sure/
 
Upvote 0
Thanks JLGWhiz! I removed the line : Application.Goto Reference:="Macro1"
and now the header row isn't overwritten. The thing is, I don't have a "Macro1" so I'm surprised an error wasn't thrown. I had that line leftover from a very early attempt. Thanks again!
 
Upvote 0
Thanks JLGWhiz! I removed the line : Application.Goto Reference:="Macro1"
and now the header row isn't overwritten. The thing is, I don't have a "Macro1" so I'm surprised an error wasn't thrown. I had that line leftover from a very early attempt. Thanks again!
Glad you worked it out.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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