KP_SoCal
asked on
VBA to insert rows
I've posted a thread that is related to this question, but I want to bring it in a different direction and add a little more clarity behind what I'm trying to accomplish.
I’m looking for a code that will select a specific range of worksheets that includes: ‘Summary’, ‘Details’, and sheet names that start with ‘Zone’, and insert a specified number of rows immediately below the row with the active cell. The number of rows to insert should be referenced from a message input box that requires a user to type how many rows are required to be inserted.
Next it should copy the contents of the row containing the active cell and paste them into the rows that were just inserted. The reason for inserting prior to copying is to ensure the formulas that reference other worksheets refer to the correct cell row. If I were to copy and then insert, the formulas would not update correct.
If I were to manually execute this without a macro, the steps would go like this:
1. In the attached file, notice Summary!A8 has a formula of SUM('Zone XY:Zone 8B'!A8)
2. Select the following sheets: ‘Summary’, ‘Details’, ‘Zone XY’, ‘Zone 12’, and ‘Zone 8B’
3. Select rows 5, 6, 7 to insert three rows below row 4
4. Copy row 4 and pasted into rows 5, 6, 7
5. Now Summary!A11 has a formula of SUM('Zone XY:Zone 8B'!A11)
That’s the logic, but I just can't piece together the right code that will execute it properly. This is a tough one. Thanks in advance for your help.
P.S. A really nice macro to select the specified worksheets is listed below (compliments of MatthewsPatrick)
Sample-File.xlsx
I’m looking for a code that will select a specific range of worksheets that includes: ‘Summary’, ‘Details’, and sheet names that start with ‘Zone’, and insert a specified number of rows immediately below the row with the active cell. The number of rows to insert should be referenced from a message input box that requires a user to type how many rows are required to be inserted.
Next it should copy the contents of the row containing the active cell and paste them into the rows that were just inserted. The reason for inserting prior to copying is to ensure the formulas that reference other worksheets refer to the correct cell row. If I were to copy and then insert, the formulas would not update correct.
If I were to manually execute this without a macro, the steps would go like this:
1. In the attached file, notice Summary!A8 has a formula of SUM('Zone XY:Zone 8B'!A8)
2. Select the following sheets: ‘Summary’, ‘Details’, ‘Zone XY’, ‘Zone 12’, and ‘Zone 8B’
3. Select rows 5, 6, 7 to insert three rows below row 4
4. Copy row 4 and pasted into rows 5, 6, 7
5. Now Summary!A11 has a formula of SUM('Zone XY:Zone 8B'!A11)
That’s the logic, but I just can't piece together the right code that will execute it properly. This is a tough one. Thanks in advance for your help.
P.S. A really nice macro to select the specified worksheets is listed below (compliments of MatthewsPatrick)
Dim MyList As String
Dim arr As Variant
Dim ws As Worksheet
Application.ScreenUpdating = False
MyList = "Summary,Details"
With ThisWorkbook
For Each ws In .Worksheets
If LCase(ws.Name) Like "zone*" Then
MyList = MyList & "," & ws.Name
End If
Next
arr = Split(MyList, ",")
.Worksheets(arr).Select
.Worksheets("Summary").Activate
End With
Sample-File.xlsx
ASKER
Hi rspahitz. Thank you for replying. I'm familiar with absolute and relative references. The issue is I'm running into is that relative references are behaving like an absolute reference when rows are copied inserted.
The easiest way to understand this is open my attached file call "Example Code.xlsm". And perform the following steps:
Part A
1. Select cell Summary!A8 and you'll see the following forumla displayed: SUM('Zone XY:Zone 8B'!A8)
2. Next select the following tabs by clicking on them: 'Summary', 'Details', and all 'Zone' tabs
3. Then insert 5 rows between rows 2 and 3
4. Now you'll notice the original row 8 was pushed down to row 13
5. When you view the formula in cell A13, it will be correctly displayed as: SUM('Zone XY:Zone 8B'!A13)
6. Close the file
Part B
1. Open file
2. Select cell Summary!A4
3. Press [Alt][F11] on your keyboard to bring up the IDE
4. Run public sub process
5. In the dialog box, key in the number 5
6. Now if you go down to cell A13, you see the formula is incorrectly displayed as: SUM('Zone XY:Zone 8B'!A8)
It should be A13 and not A8. I hope this sheds some light on this issue. Thanks for your help.
Example.xlsx
The easiest way to understand this is open my attached file call "Example Code.xlsm". And perform the following steps:
Part A
1. Select cell Summary!A8 and you'll see the following forumla displayed: SUM('Zone XY:Zone 8B'!A8)
2. Next select the following tabs by clicking on them: 'Summary', 'Details', and all 'Zone' tabs
3. Then insert 5 rows between rows 2 and 3
4. Now you'll notice the original row 8 was pushed down to row 13
5. When you view the formula in cell A13, it will be correctly displayed as: SUM('Zone XY:Zone 8B'!A13)
6. Close the file
Part B
1. Open file
2. Select cell Summary!A4
3. Press [Alt][F11] on your keyboard to bring up the IDE
4. Run public sub process
5. In the dialog box, key in the number 5
6. Now if you go down to cell A13, you see the formula is incorrectly displayed as: SUM('Zone XY:Zone 8B'!A8)
It should be A13 and not A8. I hope this sheds some light on this issue. Thanks for your help.
Example.xlsx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW this is one of the few times I believe you really have to Select things and use the selection object in VBA code. I would normally avoid using Selection in VBA because it is slower and introduces all sorts of dependencies on what's visble, active, slected, etc. However in this case, because VBA does not 'do' 3d ranges, you have to select and fool Excel into thinking it's all coming through the user interface.
ASKER
Excellent! I'm extremely dissappointed that I can't award you 1000 points for this! Thanks again!!!
ASKER
Also, I just noticed that I attached the wrong file. So even more kudos to you for piecing this together without having the correct example file. ;-)
If you want formulas to copy without changing references, you can change them to absolute references (put $ before the column and row references, like $C$12) Then when you copy and paste, the references remain exactly the same.
Recording a macro to insert a line is pretty simple. Let me know if you need help with that.
Next, adding a "popup" box to ask for how many rows is a tiny bit more challenging, but not that bad.
If you want me to continue, I'll take a look at your attachment, but I'll also let MatthewPatrick continue with this if he sees it.