1

What I am trying to do:

Exporting excel doc from Salesforce. I am trying to delete some rows based on a condition, then add rows based on a different set of conditions. Being new to VBA, I've browsed the forums and googled some sample codes, but cannot get them to work fully.

Adding rows:

Column C = Quantity. Need to add rows below based on the value in column C. If quantity = 3 - I want to add 2 blank rows below. If quantity = 4 - 3 blank rows below...

Tried:

Sub BlankLine()

Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long

    Col = "C"
    StartRow = 15
    BlankRows = 1

        LastRow = Cells(Rows.Count, Col).End(xlUp).Row

        Application.ScreenUpdating = False

        With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) > 1 Then
.Cells(R + 1, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
Application.ScreenUpdating = True

End Sub

This works, but only adds a single row below - I need it to add rows based on the value of column C = 'quantity'.

Deleting Rows:

Column A = Product Names. If any product name contains MAINT or APP, I need to delete that row. Tried:

Sub test()

With ActiveSheet
.AutoFilterMode = False
With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .AutoFilter 1, "*Maint*"
    On Error Resume Next
    .Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
End With
End Sub

This also works, but deletes the top row once done, so I lose some information.

Any suggestions on how to approach this is greatly appreciated!

7
  • add rows based on the value of column C please explain. Commented Jun 8, 2016 at 19:44
  • If for example C4 has a quantity of '4' - I'd like to add 3 empty rows below Commented Jun 8, 2016 at 20:13
  • Well the code would hopefully loop down to the next 'not empty' cell, so for example if C5 = 2 then I'd want to add 1 empty row below Commented Jun 8, 2016 at 20:19
  • Ahhh, now I have an idea of what the data looks like... Commented Jun 8, 2016 at 20:21
  • What Abe has below should be close enough. Commented Jun 8, 2016 at 20:37

2 Answers 2

0

As for inserting rows

For R = LastRow To StartRow + 1 Step -1
   With .Cells(R, Col)
      If .Value > 1 Then .Offset(1).Resize(.Value).EntireRow.Insert
   End With
Next R

While for filtering and deleting

Sub test()
With ActiveSheet
   .AutoFilterMode = False
   With .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
      .AutoFilter Field:=1, Criteria1:=Array("*Maint*", "*APP*"), Operator:=xlFilterValues
      If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Offset(1).Resize(.Rows.Count -1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
   End With
   .AutoFilterMode = False
End With
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

@bliq did you get through it?
0
If .Cells(R, Col) > 1 Then
   numRows = val(.Cells(R, Col)) - 1
   For i = 1 to numRows
       .Cells(R + 1, Col).EntireRow.Insert Shift:=xlDown
   Next i
End If

2 Comments

Im getting an error for this - on the row "numRows = val(.Cells(R, Col) - 1". Could you maybe post the entire code you'd use - thanks!
Oops. Left out a closing parentheses. See above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.