Adding rows by value location, excel crash

Deverti

Board Regular
Joined
Sep 5, 2020
Messages
63
Office Version
  1. 365
Platform
  1. Windows
This probably isnt the the most appropriate solution to begin with but it gets the job done, almost.

Thing is, "BufferBelow" works as intended while "BufferAbove" leads excel into a crash.

What s the big difference? i dont understand ~

VBA Code:
Sub BufferBelow()
Application.ScreenUpdating = False
With ActiveSheet
    Dim vRb As Range
  
   For Each vRb In Range("A1", Range("A" & Rows.Count).End(xlUp))
      Select Case vRb.Value
         Case "Aufwand", "Ertrag"
            vRb.Offset(0, 1).EntireRow.Insert
      End Select
   Next vRb
End With
Application.ScreenUpdating = True
End Sub

Sub BufferAbove()
Application.ScreenUpdating = False
With ActiveSheet
    Dim vRa As Range
  
   For Each vRa In Range("A1", Range("A" & Rows.Count).End(xlUp))
      Select Case vRa.Value
         Case "Total Aufwand", "Gewinn", "Total Ertrag"
            vRa.Offset(0, -1).EntireRow.Insert
      End Select
   Next vRa
End With
Application.ScreenUpdating = True
End Sub
 
Ok, now im very thankful for your help but also quite confused :x
A1 is empty and i did see your request but, with my lack of knowledge here i simply assumed it was a typo, my bad ~

If i use your code with Offset(0), or without it ofc, it does exactly what i need, which is awesome hehe.

however, i now tried it 3 times with Offset(-1) and during these attempts it
1: interacted with "Total Aufwand" as it did without offset; ignored "Gewinn"; added a row one row above where it does without offset on "Total Ertrag"
2: ignored "Total Aufwand"; interacted with "Gewinn" as it did without offset; ignored"Total Ertrag"
3: same as "2:"

so make with that what you will, just figured you might care to know. my current issue is solved, thanks a bunch^^
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
No surprise at all, I was waiting for you to say it wasn't doing what you want with -1 offset as Insert (without an offset) already puts it in the line above the current row.

I just left it how you had it because you didn't explain in your descriptions what you wanted the code to actually do.
 
Upvote 0
i meant, confused because it seems that using Offset(-1) resulted in random interactions with the defined cases

ofc it s most likely that i somehow managed to screw with it without realizing having done so but as far as my perception goes i legit used the exact same code with varying returns
 
Upvote 0
Nothing random, it is because it is inserting a blank row 2 up and so throws off the count in the loop, you get the opposite effect when deleting rows from top to bottom.
 
Upvote 0
Example say you went to do your -1 insert on row 10 i.e. you start with
Book1
A
11
22
33
44
55
66
77
88
99
1010
Sheet1

Then you do your -1 insert you get
Book1
A
11
22
33
44
55
66
77
88
9
109
1110
Sheet1

so what was originally row 9 is now row 10 and so will be skipped in the loop as it goes from the original row 10 to the new row 9.
 
Upvote 0
oh man, should ve realized that myself, didnt know the original count would serve as basis for everything while neglecting any additional content that would interfere with the count.
getting it done bottom up isnt a mystery anymore either, jesus. thanks for clearing that up
 
Upvote 0

Forum statistics

Threads
1,216,025
Messages
6,128,352
Members
449,443
Latest member
Chrissy_M

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