RegularExcelUser
New Member
- Joined
- Apr 6, 2023
- Messages
- 25
- Office Version
- 365
- Platform
- Windows
Hi there
I need some help with relative references. The code below is a macro I recorded with fixed references, and it works fine e.g. when I position my cursor at a certain point, it will add 9 rows above it, copy data from the 9 rows above, paste it into the new 9 rows, and then modify some formulas and text. So far, so good. When I try recording the same actions in a macro with relative references, it doesn't work e.g. the cell references are all over the shop, it ends up inserting the new rows much further up my sheet, tries to make edits to other unrelated cells, etc. This is irrespective of whether I start in the cell above row 106, or in row 106 (I thought that might be introducing some issue with negative R1s). To that end, I'd rather just fix the code below to put the R1C1 references in, but not sure how to do that e.g. do I need to make some adjustment to the variables "Rows" or "Range" to make the R1C1 work for single cell/multiple cell references. To make this a little easier, pieces in particular that I'm interested in understanding how to amend are Rows("106:114").Select, Range("B105").Select and Range("I97:J97").Select. If I can wrap my head around these 3, I can figure out the rest myself.
I need some help with relative references. The code below is a macro I recorded with fixed references, and it works fine e.g. when I position my cursor at a certain point, it will add 9 rows above it, copy data from the 9 rows above, paste it into the new 9 rows, and then modify some formulas and text. So far, so good. When I try recording the same actions in a macro with relative references, it doesn't work e.g. the cell references are all over the shop, it ends up inserting the new rows much further up my sheet, tries to make edits to other unrelated cells, etc. This is irrespective of whether I start in the cell above row 106, or in row 106 (I thought that might be introducing some issue with negative R1s). To that end, I'd rather just fix the code below to put the R1C1 references in, but not sure how to do that e.g. do I need to make some adjustment to the variables "Rows" or "Range" to make the R1C1 work for single cell/multiple cell references. To make this a little easier, pieces in particular that I'm interested in understanding how to amend are Rows("106:114").Select, Range("B105").Select and Range("I97:J97").Select. If I can wrap my head around these 3, I can figure out the rest myself.
VBA Code:
Sub TEST_OF_ADDING_MULTIPLE_ROWS()
'
' TEST_OF_ADDING_MULTIPLE_ROWS Macro
'
'
Rows("106:114").Select
Selection.Insert Shift:=xlDown
Range("B105").Select
Selection.Copy
Range("B106").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A106").Select
ActiveCell.FormulaR1C1 = "'Period"
Range("B106").Select
Selection.Copy
Range("B107").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A107").Select
ActiveCell.FormulaR1C1 = "'Amount"
Range("B107").Select
Selection.Copy
Range("B108").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A108").Select
ActiveCell.FormulaR1C1 = "'Amount"
Range("A108").Select
Selection.InsertIndent 1
Range("B108").Select
Selection.Copy
Range("B109").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A109").Select
ActiveCell.FormulaR1C1 = "'Contract Penalty Amount"
Range("B109").Select
Selection.Copy
Range("B110").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A110").Select
ActiveCell.FormulaR1C1 = "'Contract Penalty Amount"
Range("A110").Select
Selection.InsertIndent 1
Range("B110").Select
Selection.Copy
Range("B111").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A111").Select
ActiveCell.FormulaR1C1 = "'-14 Days"
Range("B111").Select
Selection.Copy
Range("B112").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A112").Select
ActiveCell.FormulaR1C1 = "'- 7 Days"
Range("B112").Select
Selection.Copy
Range("B113").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A113").Select
ActiveCell.FormulaR1C1 = "'-1 Day"
Range("B113").Select
Selection.Copy
Range("B114").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A114").Select
ActiveCell.FormulaR1C1 = "'Add Period"
Range("H97").Select
Selection.Copy
Range("H106").Select
ActiveSheet.Paste
[SIZE=6][COLOR=rgb(44, 130, 201)]Range("I97:J97")[/COLOR][/SIZE].Select
Application.CutCopyMode = False
Selection.Copy
Range("I106").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("H98:H104").Select
Selection.Copy
Range("H107").Select
ActiveSheet.Paste
Range("I106").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Enter Start Period For Next Amount Here"
Range("J106").Select
ActiveCell.FormulaR1C1 = "Enter End Period for 1st Amount here"
Range("J106").Select
ActiveCell.FormulaR1C1 = "Enter End Period for Next Amount here"
Range("H106").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[1]=""Enter Start Period For Next Amount Here"","""",IF(AND(RC[1]<>""Enter Start Period For Next Amount Here"",RC[2]=""Enter End Period for Next Amount here""),RC[1],IF(AND(RC[1]<>""Enter Start Period For Next Amount Here"",RC[2]<>""Enter End Period for Next Amount here""),RC[1]&"" - ""&RC[2])))"
Range("H111").Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
Range("A114").Select
End Sub
Last edited: