VBA Inserting rows based on Column criteria

RoosterBull

New Member
Joined
Dec 1, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I really would appreciate help with my VBA coding as I've been struggling with it for days without a positive solution. It's pointless to show what I have managed thus far, although could be comical to some expert coders.
Based on the value of entire Column "J", I need the VBA to insert rows totaling to "10" when "J" is a "4" including those cells in "J" that are a "4".
At the same time, I require all "10" rows in column "C" to be merged and to resize the Row Height to "11"
I have uploaded an image of before running the VBA macro and the desired result after.
I really hope someone can help. Thanking you in advance.


Before VBA Macro.JPG
After VBA Macro.JPG
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Does this work for you?

VBA Code:
Sub insertRows()
    Dim lastRow As Long, i As Long, j As Long, cRow As Long, insertRows As Long
    '
    lastRow = Range("J" & Rows.Count).End(xlUp).Row
    For i = lastRow To 1 Step -1
        If Range("J" & i).Value = 4 Then
            j = 0
            cRow = i
            While Range("J" & i).Value = 4
                j = j + 1
                i = i - 1
            Wend
            insertRows = 10 - j
            Range("J" & cRow).Offset(1).Resize(insertRows).EntireRow.Insert
            Range("C" & cRow - j + 1 & ":C" & cRow - j + 10).Merge
            Rows(cRow - j + 1 & ":" & cRow - j + 10).RowHeight = 11
        End If
    Next
End Sub
 
Last edited:
Upvote 0
Does this work for you?

VBA Code:
Sub insertRows()
    Dim lastRow As Long, i As Long, j As Long, cRow As Long, insertRows As Long
    '
    lastRow = Range("J" & Rows.Count).End(xlUp).Row
    For i = lastRow To 1 Step -1
        If Range("J" & i).Value = 4 Then
            j = 0
            cRow = i
            While Range("J" & i).Value = 4
                j = j + 1
                i = i - 1
            Wend
            insertRows = 10 - j
            Range("J" & cRow).Offset(1).Resize(insertRows).EntireRow.Insert
            Range("C" & cRow - j + 1 & ":C" & cRow - j + 10).Merge
            Rows(cRow - j + 1 & ":" & cRow - j + 10).RowHeight = 11
        End If
    Next
End Sub
 
Upvote 0
Does this work for you?

VBA Code:
Sub insertRows()
    Dim lastRow As Long, i As Long, j As Long, cRow As Long, insertRows As Long
    '
    lastRow = Range("J" & Rows.Count).End(xlUp).Row
    For i = lastRow To 1 Step -1
        If Range("J" & i).Value = 4 Then
            j = 0
            cRow = i
            While Range("J" & i).Value = 4
                j = j + 1
                i = i - 1
            Wend
            insertRows = 10 - j
            Range("J" & cRow).Offset(1).Resize(insertRows).EntireRow.Insert
            Range("C" & cRow - j + 1 & ":C" & cRow - j + 10).Merge
            Rows(cRow - j + 1 & ":" & cRow - j + 10).RowHeight = 11
        End If
    Next
End Sub

Hi myall_blues
Thank you very much, it works perfectly thank you very much.
You have saved me much frustration and I wish I had posed earlier.........So many hours trying and failing all the time.
 
Upvote 0
You're welcome and thanks for the feedback. I'm glad we could help.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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