Insert multiple lines into each tab

alice9527

New Member
Joined
Jan 16, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I would like to use a macro to add multiple lines into each tab and I got this code:
VBA Code:
Sub InsertRows()
'
'
'Insert x rows


    Worksheets("Worklist").Unprotect Password:="readpads"
    Worksheets("Tech 1").Unprotect Password:="readpads"
    Worksheets("Tech 2").Unprotect Password:="readpads"
    Worksheets("Tech 3").Unprotect Password:="readpads"
    Worksheets("Reported Results").Unprotect Password:="readpads"
    
    Dim Rws As Variant
    Rws = InputBox("How many rows")
    If Rws = "" Then Exit Sub
    
    Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Sheets
    
        Rows("8:10").Select
        Selection.EntireRow.Hidden = False
        Rows("9:9").Copy
        Range("10:10").Select
        ActiveCell.EntireRow.Resize(Rws).Insert Shift:=x1Down
        Rows("9:9").Select
        Selection.EntireRow.Hidden = True
        Range("A10").Select
    
    Next ws
    
End Sub

It works fine with adding the required amount of rows, but all rows been added to the first sheets.

Could anyone please help me with this?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
It is probably because the first sheet is the active sheet and your code is working only on the active sheet.

Change the active sheet and run your code again and see what happens.

Try this slightly amended code so that the changes are made to each sheet.

I have not tested this.

VBA Code:
Sub InsertRows()
'
'
'Insert x rows


    Worksheets("Worklist").Unprotect Password:="readpads"
    Worksheets("Tech 1").Unprotect Password:="readpads"
    Worksheets("Tech 2").Unprotect Password:="readpads"
    Worksheets("Tech 3").Unprotect Password:="readpads"
    Worksheets("Reported Results").Unprotect Password:="readpads"
    
    Dim Rws As Variant
    Rws = InputBox("How many rows")
    If Rws = "" Then Exit Sub
    
    Dim ws As Worksheet
        
        For Each ws In ActiveWorkbook.Sheets
    
        ' *** Following line added. ***
        ws.Activate
        
        Rows("8:10").Select
        Selection.EntireRow.Hidden = False
        Rows("9:9").Copy
        Range("10:10").Select
        ActiveCell.EntireRow.Resize(Rws).Insert Shift:=x1Down
        Rows("9:9").Select
        Selection.EntireRow.Hidden = True
        Range("A10").Select
    
    Next ws
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

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