Set Range = Worksheet.Range(Cells(..), Cells(..)) SYNTAX

BuRnZz

New Member
Joined
Dec 9, 2020
Messages
27
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hey Mr Excel Members

I keep getting an error ("wrong number of arguments" or "method range failed for object _worksheet") on trying to add multiple different cells from different rows (row as long that can change) to a range.


VBA Code:
Dim TPsheet As Worksheet
Set TPsheet = ThisWorkbook.Worksheets("TP")

Dim rownumber as Long
I = 5

Dim LabelRange as Range
Set LabelRange = TPsheet.Range(TPsheet.Cells(rownumber, "C"), TPsheet.Cells(rownumber, "E"), TPsheet.Cells(rownumber, "G"), TPsheet.Cells(rownumber, "I"))

Im just trying to make the LabelRange different values, depending on different rownumbers. I used to go with
VBA Code:
Set LabelRange = TPsheet.Range("C" & CStr(rownumber) & "," & "E" & CStr(rownumber) & "," & "G" & CStr(rownumber) & "," & "I" & CStr(rownumber))
but that throws errors occasionally too..

Im not sure what Im doing wrong, any help would be greatly appreciated...
Already read through this: Worksheet().Range(Cell(), Cell()) syntax?
and it didnt work
 
When you get the error check what the value of Rownumber is.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
If you cannot figure it out, it might be best to post the WHOLE code, and a sampling of what your data looks like.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
When you get the error check what the value of Rownumber is.
Yup this was it, it was setting it to 0 at the end of the loop. Implementing "if rowvalue = 0 then exit sub" solved it.
Thanks!
 
Upvote 0
Glad it's sorted & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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