Apply Macro to other rows

SEASIA

New Member
Joined
Jul 23, 2022
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hello

I created a macro which copies certain cells from a row into certain cells in another sheet. Now I would like to apply the macro to the other rows.

The macro:

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

'
    Range("H4").Select
    Selection.COPY
    Sheets("SPX").Select
    Range("$D$3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("SPX Spreads").Select
    Range("J4").Select
    Application.CutCopyMode = False
    Selection.COPY
    Sheets("SPX").Select
    Range("$D$4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("SPX Spreads").Select
    Range("K4").Select
    Application.CutCopyMode = False
    Selection.COPY
    Sheets("SPX").Select
    Range("$B$7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

The cells in which are the values are copied must always be the same. In this case $D$3, $D$4 and $B$7. But with every new row from which the value gets copied, the reference to the row has to move lower too. From H4 to H5, H6 and so on. The same must be true for J4, to J5, J6... and K4 to K5, K6.... I guess I need a loop. But I can't figure out how to do. I have zero experience with VBA.

By the way, I assigned the macro to a shape I created.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
To be clear. I need a shape for the active sheet at column N for every row. To the shape the macro is assigned. Then I can select from which rows I want to copy the values to the other sheet. Should I copy the first shape to all other rows? Or isn't this the right way....
 
Upvote 0
I tried it this way;

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

'
  Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("H4:N62")) Is Nothing Then
         Range("H4").Select
    Selection.COPY
    Sheets("SPX").Select
    Range("$D$3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("SPX Spreads").Select
    Range("J4").Select
    Application.CutCopyMode = False
    Selection.COPY
    Sheets("SPX").Select
    Range("$D$4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("SPX Spreads").Select
    Range("K4").Select
    Application.CutCopyMode = False
    Selection.COPY
    Sheets("SPX").Select
    Range("$B$7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End If
End Sub

But double click a cell just shows the formula.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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