I have a macro that copy and paste rows, based on a input userbox. Those rows contain formulas but, once pasting them, they become incorrect (problem of references). I'd like to make the formulas dynamic in the code (via R1C1) or via Index function so that inserting news rows, results don't change. As you can see on the picture, some cells are refering to another sheet, some are in absolute / relative references, but at the end, only rows #19 and #20 contain formulas.
How can I adapt the code below?
I've created a thread on this website as well: https://stackoverflow.com/ques…serting-new-rows#61101725
Thanks !!
Code
- Sub Stock(nbproduits As Long)
- Dim MyN As String
- Dim i As Long, MyMarker As Long, MyM As Long, LstRW As Long
- Dim ws As Worksheet: Set ws = Stocks
- If nbproduits = 0 Then
- MyN = InputBox("How many products do you want to add?", "My Input Box") If Not IsNumeric(MyN) Then MsgBox "Entrez un nombre svp", vbCritical, "Error" Exit Sub End If
- MyN = CInt(MyN) Else MyN = CInt(nbproduits) End If
- For MyMarker = 1 To 1
- LstRW = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
- MyM = Application.Match("Marker" & MyMarker, ws.Range(ws.Cells(1, 1), ws.Cells(LstRW, 1)), 0)
- For i = 1 To MyN
- ws.Rows(MyM + 1 & ":" & MyM + 6).Copy
- ws.Rows(MyM + 1 + 6 * i).EntireRow.Insert Shift:=xlUp
- ws.Rows(MyM + 1 + 6 * i).PasteSpecial Paste:=xlPasteFormats
- 'ws.Cells(MyM + 1 + 6 * i, 4).FormulaR1C1 = "R[12]C* RC[-1])" testing
- 'ws.Cells(MyM + 1 + 6 * i, 4).FormulaR1C1 = "RC[12]* RC[-1])" testing
- Next i Next MyMarker
- End Sub