using the copy and paste link into different worksheets using variables

stephicohu

New Member
Joined
Jan 27, 2023
Messages
32
Office Version
  1. 365
Platform
  1. MacOS
This is me again, what I did was take out all the .select and .activate from my subroutine and the code is shown below:


VBA Code:
Sub FillingColumnSum(ByVal SourceMonth As String, ByRef TargetMonth As String, ByVal FirstRow1 As Integer, _
ByVal SecondRow1 As Integer, ByVal FirstColumn As Integer, ByVal TargetColumn As Integer)

    Sheets(SourceMonth).Range(Cells(FirstRow1, FirstColumn), Cells(SecondRow1, FirstColumn)).Copy Sheets(TargetMonth).Range(Cells(FirstRow1, TargetColumn), Cells(SecondRow1, TargetColumn))
    ActiveSheet.Paste Link:=True

End Sub

I run the program and I get an error code 9 the subscript is out of range. The debug shows the values right so I must of screw up again. Can you guys help in this matter? should I use a string formula?
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I have revised the code and still got an error code 9.

VBA Code:
Sub FillingColumnSum(ByRef SourceMonth As String, ByRef TargetMonth As String, ByRef FirstRow1 As Integer, _
ByRef SecondRow1 As Integer, ByRef FirstColumn As Integer, ByRef TargetColumn As Integer)

    Sheets(SourceMonth).Range(Cells(FirstRow1, FirstColumn), Cells(SecondRow1, FirstColumn)).Copy _
    Destination:=Sheets(TargetMonth).Range(Cells(FirstRow1, TargetColumn), Cells(SecondRow1, TargetColumn))
    ActiveSheet.Paste Link:=True

End Sub
 
Last edited by a moderator:
Upvote 0
I think instead of showing us a code that does not work.
Please tell us with specifics what you're wanting the script to do.
 
Upvote 0
You need to qualify all the cells ranges like
VBA Code:
Sub FillingColumnSum(ByRef SourceMonth As String, ByRef TargetMonth As String, ByRef FirstRow1 As Integer, _
ByRef SecondRow1 As Integer, ByRef FirstColumn As Integer, ByRef TargetColumn As Integer)

    With Sheets(SourceMonth)
      .Range(.Cells(FirstRow1, FirstColumn), .Cells(SecondRow1, FirstColumn)).Copy _
         Sheets(TargetMonth).Range(Sheets(TargetMonth).Cells(FirstRow1, TargetColumn), Sheets(TargetMonth).Cells(SecondRow1, TargetColumn))
    End With
    ActiveSheet.Paste Link:=True
End Sub
 
Upvote 0
I have copy your example and the subroutine stills errors out. It gives me: "run-time error 9, subscript out of range." Does this needs to be a function?
 
Upvote 0
What the script is supposed to do is copy a column of sums from cell(3,33) to cell(7,33). Then, using paste link them to another sheet. I want to use variables because I am repeating the code for different sets rows of the same column later.
 
Upvote 0
If you are still getting a subscript out of range error, it suggests that one of the sheet names is wrong.
 
Upvote 0
Thank you, It works..... what the problem was the name of the sheet didn't have a space between the word and the number. Thanks again
 
Upvote 0
Glad it's sorted & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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