VBA Macro

snags

New Member
Joined
Apr 2, 2022
Messages
4
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. MacOS
Hi,

I am trying to add multiple blank rows to process my data quickly. I have having trouble to set a limit to the inserting of rows.

Example raw data:

Text
data
data

Text
data
Text
data
data
data
data

Text
Text

Needs to be:

Text

data
data
row
row
row
row

Text
data
row
row
row
row
row

Text (etc,etc,)

The Cell Value is ''Text'' and I would need 6 rows under cell value. There are other data under the Cell Value, these would need to stay under each Cell Value but the total rows between the Cell Values should always be 6 including the data.

I have gotten far with adding 6 rows with Resize(6).insert but this excludes the data, which then gives me more than 6 rows.

I would gladly like to know the rest of the steps.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
@snags Try this.

VBA Code:
Sub Ins_Rws()

Application.ScreenUpdating = False
   Set Strt = Range("A" & Rows.Count)  '<< edit column to suit
  Cells.Find(What:="Text", After:=Strt, LookIn:=xlFormulas2, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
   r = ActiveCell.Row
   fst = r
   GoTo First:
   
Do Until c = fst
   Cells.FindNext(After:=ActiveCell).Activate
    
    r = ActiveCell.Row
  
First:
    c = Cells.FindNext(After:=ActiveCell).Row
    
    Ins = 7 - c + r
    If Not Ins = 0 And Not Ins > 6 Then Cells(c, 1).Resize(Ins, 1).EntireRow.Insert
   
   Loop
Application.ScreenUpdating = True
   End Sub

Hope that helps.
 
Upvote 0
Another option - assumes your data is in Column A and that Column B is available to use.

VBA Code:
Option Explicit
Sub Insert6()
    Dim i As Long, j As Long, k As Long, x As Long
    Dim a, b, c, z
    Application.ScreenUpdating = False
    a = Range("A1", Cells(Rows.Count, "A").End(3))
    ReDim b(1 To UBound(a), 1 To 1)
    ReDim c(1 To UBound(a) * 7, 1 To 1)
    
    For i = 1 To UBound(a)
        If a(i, 1) = "Text" Then
            j = j + 1
        End If
        b(i, 1) = j
    Next i
    
    Range("B1").Resize(UBound(a)).Value = b
    k = Application.Max(Range("B:B"))
    
    j = 1
    For i = 1 To k
        x = 7 - Application.CountIf(Range("B:B"), i)
        For z = 1 To x
            c(j, 1) = i
            j = j + 1
        Next z
    Next i
    
    Cells(UBound(b) + 1, 2).Resize(UBound(c)).Value = c
    Range("A1", Cells(Rows.Count, "B").End(3)).Sort key1:=Range("B1"), order1:=xlAscending, Header:=xlNo
    Range("B:B").ClearContents
End Sub

Before
text rows.xlsb
AB
1Text
2data
3data
4Text
5data
6Text
7data
8data
9data
10data
11Text
12Text
13
Sheet1


After
text rows.xlsb
AB
1Text
2data
3data
4
5
6
7
8Text
9data
10
11
12
13
14
15Text
16data
17data
18data
19data
20
21
22Text
23
24
25
26
27
28
29Text
30
31
32
33
34
35
Sheet1
 
Upvote 0
Solution
@snags Try this.

VBA Code:
Sub Ins_Rws()

Application.ScreenUpdating = False
   Set Strt = Range("A" & Rows.Count)  '<< edit column to suit
  Cells.Find(What:="Text", After:=Strt, LookIn:=xlFormulas2, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
   r = ActiveCell.Row
   fst = r
   GoTo First:
  
Do Until c = fst
   Cells.FindNext(After:=ActiveCell).Activate
   
    r = ActiveCell.Row
 
First:
    c = Cells.FindNext(After:=ActiveCell).Row
   
    Ins = 7 - c + r
    If Not Ins = 0 And Not Ins > 6 Then Cells(c, 1).Resize(Ins, 1).EntireRow.Insert
  
   Loop
Application.ScreenUpdating = True
   End Sub

Hope that helps.
Hi Snakehips, I have tried this code but it got rejected , code 91 something like that.

Thank you very much!
 
Upvote 0
Another option - assumes your data is in Column A and that Column B is available to use.

VBA Code:
Option Explicit
Sub Insert6()
    Dim i As Long, j As Long, k As Long, x As Long
    Dim a, b, c, z
    Application.ScreenUpdating = False
    a = Range("A1", Cells(Rows.Count, "A").End(3))
    ReDim b(1 To UBound(a), 1 To 1)
    ReDim c(1 To UBound(a) * 7, 1 To 1)
   
    For i = 1 To UBound(a)
        If a(i, 1) = "Text" Then
            j = j + 1
        End If
        b(i, 1) = j
    Next i
   
    Range("B1").Resize(UBound(a)).Value = b
    k = Application.Max(Range("B:B"))
   
    j = 1
    For i = 1 To k
        x = 7 - Application.CountIf(Range("B:B"), i)
        For z = 1 To x
            c(j, 1) = i
            j = j + 1
        Next z
    Next i
   
    Cells(UBound(b) + 1, 2).Resize(UBound(c)).Value = c
    Range("A1", Cells(Rows.Count, "B").End(3)).Sort key1:=Range("B1"), order1:=xlAscending, Header:=xlNo
    Range("B:B").ClearContents
End Sub

Before
text rows.xlsb
AB
1Text
2data
3data
4Text
5data
6Text
7data
8data
9data
10data
11Text
12Text
13
Sheet1


After
text rows.xlsb
AB
1Text
2data
3data
4
5
6
7
8Text
9data
10
11
12
13
14
15Text
16data
17data
18data
19data
20
21
22Text
23
24
25
26
27
28
29Text
30
31
32
33
34
35
Sheet1
Hi Kevin9999,

Awesome, this has worked. Thank you very much!

Do you know if it is also possible for instead Text that it find a certain character in the cell? Lets say this character '' ; '' ?
 
Upvote 0
Hi Kevin9999,

Awesome, this has worked. Thank you very much!

Do you know if it is also possible for instead Text that it find a certain character in the cell? Lets say this character '' ; '' ?
Yes, that should be possible. Test it by changing this line:

VBA Code:
If a(i, 1) = "Text" Then

to this

VBA Code:
If a(i, 1) Like "*;*" Then

and let me know how you go.
 
Upvote 0
Yes, that should be possible. Test it by changing this line:

VBA Code:
If a(i, 1) = "Text" Then

to this

VBA Code:
If a(i, 1) Like "*;*" Then

and let me know how you go.
Hi Kevin9999,

It worked thank very much!
 
Upvote 0

Forum statistics

Threads
1,223,792
Messages
6,174,608
Members
452,574
Latest member
hang_and_bang

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