Macro to cope row above and replace some numbers

rskn

New Member
Joined
May 19, 2021
Messages
3
Office Version
  1. 365
Hi all

This is my first time here, and im not very good with scripting, so bear with me :)

I'm trying to make a macro that copies a selected row and then changes a few values.
1621422956382.png

My macro so far is this:

VBA Code:
Sub New_Revision2()
'
' New_Revision2 Macro
'

'
    ActiveCell.EntireRow.Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
    Range("C" & (ActiveCell.Row)).Select
    ActiveCell.FormulaR1C1 = "=R[-1]C"
    Application.CutCopyMode = False
    Selection.ClearContents
    Range("U" & (ActiveCell.Row)).Select
    ActiveCell.FormulaR1C1 = "=R[-1]C+1"
    Range("V" & (ActiveCell.Row)).Select
    ActiveCell.FormulaR1C1 = "=R3C2"
    Intersect(Range("U:V"), ActiveCell.EntireRow).Select
    ActiveCell.FormulaR1C1 = "=R[1]C"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

This is what happens when i run the macro.
I can copy the selected row, and the "X" is removed. But i cant get the shown data below to be updated correctly.
1621423436981.png


I hope anyone can help me? :)
 
Last edited by a moderator:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
VBA Code:
Sub rskn()
   Dim Cl As Range
   
   Set Cl = ActiveCell
   Cl.Offset(1).EntireRow.Insert
   Cl.EntireRow.Resize(2).FillDown
   With Range("C" & Cl.Row)
      .Offset(1).FormulaR1C1 = "=R[-1]C"
      .ClearContents
   End With
   With Range("U" & Cl.Row)
      .Offset(1).Value = .Value + 1
   End With
   Range("V" & Cl.Row).Offset(1).Value = Range("B3").Value
End Sub
 
Upvote 0
How about
VBA Code:
Sub rskn()
   Dim Cl As Range
  
   Set Cl = ActiveCell
   Cl.Offset(1).EntireRow.Insert
   Cl.EntireRow.Resize(2).FillDown
   With Range("C" & Cl.Row)
      .Offset(1).FormulaR1C1 = "=R[-1]C"
      .ClearContents
   End With
   With Range("U" & Cl.Row)
      .Offset(1).Value = .Value + 1
   End With
   Range("V" & Cl.Row).Offset(1).Value = Range("B3").Value
End Sub
Hi Fluff

Thanks it works somewhat.
1. it makes a 0 instead of a X:
1621430039311.png

2. I have a reference on another sheet for ex. row 1198. when i run the macro it needs to keep the reference on the other sheet to the new row created by the macro.
I don't know if it makes sense :)
When i do it manually now i select the row -> Copy -> Select the same row and then "insert copied cells". I guess this inserts the cells above, and by doing that, keeps the reference on the other sheets.

1621430219151.png
 
Upvote 0
Ok, how about
VBA Code:
Sub rskn()
   Dim Cl As Range
   
   Set Cl = ActiveCell
   Cl.EntireRow.Copy
   Cl.EntireRow.Insert
'   Cl.EntireRow.Resize(2).FillDown
   With Range("C" & Cl.Row)
      .Value = .Offset(-1).Value
      .Offset(-1).ClearContents
   End With
   With Range("U" & Cl.Row)
      .Value = .Offset(-1).Value + 1
   End With
   Range("V" & Cl.Row).Value = Range("B3").Value
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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