Not sure how the number 0 was added to a cell when using a macro to add a blank row to excel file VBA

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I have this macro that adds an empty row (only column A will contain a value same as the cell above it) between two rows that contain different values in column B
in the example file below, a new row is added after rows: 14, 20, 27. The code is doing everything that it was intended to do except it adds "0" to column K in the new row...
any idea what is causing this ? Thanks !


1673891600706.png


1673891808677.png


VBA Code:
Sub BlankSpaceBetweenStructures()
    Dim lr As Long, r As Long
    Application.ScreenUpdating = False

    lr = Cells(Rows.Count, "B").End(xlUp).Row

    For r = lr To 3 Step -1

        If Cells(r, "B").Value <> Cells(r - 1, "B").Value Then

            Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

            Cells(r, "A").Value = Cells(r - 1, "A").Value
        End If
    Next r
    Application.ScreenUpdating = True
End Sub
 

Attachments

  • 1673891551558.png
    1673891551558.png
    142.7 KB · Views: 10
I am glad you found it.

Your latest query is a different question altogether (evaluating a whole different bit of code), and it would be best to post that in a new thread as its own question.
(That is a not a method I use at all, so I probably would not be the best person to answer this question anyhow).
Okay Joe, I will ask in another post. Thanks for your help on troubleshooting this
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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