Ranges "Set" = I'm Lost

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
Daily I copy two worksheets into a new workbook. The worksheets names change daily so I'm trying to reference the sheets by index number and get the value from Cells(1 , 1). But I'm not getting the ranges set properly for the reference/copy from cells and can't figure it out. My initial attempt was to use "With" but I couldn't get that written properly either.

TIA

Code:
Sub tm_PastUser()'
   Sheet1.Activate
   '
   Dim tCell    As Range
   Dim SumCell  As Range
   '
   Dim ThisWb   As Workbook
   '
   Dim ThisWs   As Worksheet
   Dim OtherWs  As Worksheet
   '
   Set ThisWs = Worksheets(1)
   Set OtheWs = Worksheets(2)
   Set tCell = ThisWs.Cells(2, 5)
   'Set SumCell = ThisWb.OtherWs.Cells(1, 1)
   Set SumCell = ThisWb.Range("OtherWs").Range(Cells(1, 1))
   '
   SumCell.Copy tCell
  Application.CutCopyMode = False
End Sub

As always, I appreciate the help.

Ron
 
Sorry, this was buried earlier on: "Object Variable or With Block Variable Not Set" error. When I hover over the code, I'm shown in a field the proper information. Sorry I don't know what that's called and I don't seem to have the right type of place to park a picture so I can upload it . . .
view
view
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This works for me
Code:
Sub tm_PastUser() '
   'Sheet1.Activate
   '
   Dim tCell    As Range
   Dim SumCell  As Range
   '
   Dim ThisWb   As Workbook
   '
   Dim ThisWs   As Worksheet
   Dim OtherWs  As Worksheet
   '
   Set ThisWs = Worksheets(1)
   Set OtherWs = Worksheets(2)
   Set tCell = ThisWs.Cells(2, 5)
   Set SumCell = OtherWs.Cells(1, 1)
   '
   SumCell.Copy tCell
  Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
Thanks! And I figured out my dumb error. :banghead: Then the power went out! I created a new workbook, saved as a .xlsm, and pasted in your code and two worksheets. Was surprised to see when I copied in the two worksheets that they came in as Sheet4 & sheet5. Duh, Blinding Flash of the Obvious . . . I'd confused Index Number Sheet Number :mad:

Sheet1 is the sheet number
Worksheets(1) is the Worksheet Index Number
Worksheets("Sheet1") "Sheet1" is the sheet Name


Half way through my troubleshooting I'd changed from sheet number to index number.
NB for others reading this later: Index number = worksheet sequence. That changes anytime a worksheet is moved in a workbook, and it can only be changed by changing a worksheet's order in the workbook). A quick test shows that when worksheets are copied into a workbook they either retrain their sheet number, sheet5, even if there isn't a sheet4, but will be given the next sheet number if there is a conflict.

When the code was corrected it was rebroken due to my trying to use a nonexistent sheet Index number.
 
Upvote 0
Glad it's sorted & thanks for the feedback.

For reference Sheet1 is not the sheet number, it's the sheet codename.
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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