VBA code modification to insert row to a table instead of entire worksheet

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Is there a way to add only a range of cells instead of entire row? Here, I will like to insert from Col B to Col O. I have other data adjacent the range B:O and the entire row is interfering with the layout. I will be more than happy to find a work around. Thanks in advance.
Code:
        .Rows(i).Insert xlUp


Full code is as below


Code:
Sub InsertRowsThenAdd()
Dim i&, j&, k&, n&, q&, x&, r As Range, va, y As Double, z As Double, lr&
Application.ScreenUpdating = False
On Error Resume Next
With Sheets("Sheet1")
        lr = .Range("D" & .Rows.Count).End(xlUp).Row
    For i = lr To 4 Step -1
        Do While .Cells(i, "D") = .Cells(i - 1, "D")
            i = i - 1
        Loop
        If i = 4 Then Exit For
        .Rows(i).Insert xlUp
    Next i
    n = .Range("D" & .Rows.Count).End(xlUp).Row
    q = 4
    For Each r In .Range("D4:D" & n + 1).SpecialCells(xlCellTypeBlanks)
        For k = 5 To 15 Step 2
            x = r.Row
            .Cells(x, k) = WorksheetFunction.SumIf(.Range(.Cells(q, k), .Cells(x - 1, k)), ">0")
            Select Case k
                Case 5, 7, 9, 11, 13
                    y = y + .Cells(x, k)
                Case 15
                    If .Cells(x, k) > 0 Then
                        z = z + .Cells(x, k)
                    End If
            End Select
            .Cells(x, k).Font.Bold = True
            .Cells(x, k).Font.Color = RGB(0, 128, 128)
            .Cells(x, 3).Font.Bold = True
            .Cells(x, 3).Font.Color = RGB(0, 128, 128)
            .Cells(x, 3) = "SUB-TOTAL"
        Next k
        q = x + 1
    Next r
    .[M2] = y: .[O2] = z
    Application.ScreenUpdating = True
    Exit Sub
    On Error GoTo 0
End With
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try making this change
Code:
<del>.Rows(i).Insert xlUp</del>
.Range("B" & i & ":O" & i).Insert Shift:=xlDown
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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