Pasting results of an automated calculation into a different sheet

Bamh1

New Member
Joined
Oct 7, 2021
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
Hello,

I have a sub called abc which does some calculations and returns the results in the range b2:i2 of Sheet1. I want to run this sub multiple times, and each time paste the results in Sheet2 such that next results will be pasted in the row below the previous one. I have written the following code, but it doesn't work:

Sub abc_sim ()
Dim i As Integer, c As Integer
Dim rs1 As Range, rs2 As Range

c = 1
rs1 = Worksheets("Sheet1").Range("b2:i2")
rs2 = Worksheets("Sheet2").Range("b2:i2")

For i = 1 To 100
Call abc
rs1.Copy
rs2.Offset(c, 0).PasteSpecial Paste:=xlPasteValues
c = c + 1
Next i

End Sub


Could someone please help fixing this code.


Thank you,


Shawn
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hello, Shawn.
Please try this version.

VBA Code:
Sub abc_sim()
 Dim i As Long
  Application.ScreenUpdating = False
  For i = 1 To 100
   Call abc
   Sheets("Sheet1").[B1:I1].Copy
   Sheets("Sheet2").Cells(Rows.Count, 2).End(3)(2).PasteSpecial xlValues
  Next i
End Sub
 
Upvote 0
Solution
Hello, Shawn.
Please try this version.

VBA Code:
Sub abc_sim()
 Dim i As Long
  Application.ScreenUpdating = False
  For i = 1 To 100
   Call abc
   Sheets("Sheet1").[B1:I1].Copy
   Sheets("Sheet2").Cells(Rows.Count, 2).End(3)(2).PasteSpecial xlValues
  Next i
End Sub
Hi Osvaldo, Thank you for your response. I tried it, but I get a runtime error (script out of range), and when I debug the line that has the paste values is highlighted. Any suggestions?

Thank you,
 
Upvote 0
Please, make sure your sheet name is spelled exactly as Sheet2, like in the code, with no spaces before or after that name.
 
Upvote 0
Please, make sure your sheet name is spelled exactly as Sheet2, like in the code, with no spaces before or after that name.
It worked. Thank you so much.
To understand this code better, could you be kind enough to tell me what those two parameters after .end (2)(3) do?
 
Upvote 0
The (3) is a shorthand method of referring to the xlDirection (as far as I know it is undocumented), used rather than the official enumeration (see the table below)

Shorthand EnumerationConstantOfficial enumeration
1xlToLeft-4159
2xlToRight-4161
3xlUp-4162
4xlDown-4121


The (2) refers to the Range.Item property. See the article in the link below by Alan Beban (republished by Chip Pearson) which explains it a bit better than Microsoft's page does.
I'd read it all but the single indexing method section is what is being used here to Offset the cell by one row.

 
Last edited:
Upvote 0
The (3) is a shorthand method of referring to the xlDirection (as far as I know it is undocumented), used rather than the official enumeration (see the table below)

Shorthand EnumerationConstantOfficial enumeration
1xlToLeft-4159
2xlToRight-4161
3xlUp-4162
4xlDown-4121


The (2) refers to the Range.Item property. See the article in the link below by Alan Beban (republished by Chip Pearson) which explains it a bit better than Microsoft's page does.
I'd read it all but the single indexing method section is what is being used here to Offset the cell by one row.

Thank you Mark for your detailed explanation. I read the follow up comment as well.
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,563
Members
448,972
Latest member
Shantanu2024

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