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
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What are you ultimately using this "LabelRange" variable for?
I may have some other ideas on how to accomplish what you want.
 
Upvote 0
What are you ultimately using this "LabelRange" variable for?
I may have some other ideas on how to accomplish what you want.
The LabelRange is one of the ranges that I need to populate a chart with data
In the Chart-Sub it combines this range with others for the chart
VBA Code:
Chart.SetSourceData Source:=Application.Union(LabelRange, ValueRange, Xwerte)
 
Upvote 0
Where exactly are you setting the "rownumber" variable value in your code?
I am not seeing it set anywhere in the original code you posted.
 
Upvote 0
I have
VBA Code:
rownumber = 5
above but it later changes multiple times to different rows, i think I cant explain it all here but its a dynamic long value that gives me the row# which can be different each time :P
 
Upvote 0
How about
VBA Code:
Set LabelRange = Union(TPsheet.Cells(rownumber, "C"), TPsheet.Cells(rownumber, "E"), TPsheet.Cells(rownumber, "G"), TPsheet.Cells(rownumber, "I"))
 
Upvote 0
Solution
OK, in the code you posted, you had "I = 5", not "rownumber = 5", which is why I was questioning it.
You had not set the value of "rownumber" in between the time you declared it and were first using it (unless you cut out a bunch of VBA code in the middle of this block):
VBA Code:
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"))

Assuming you have the rownumber set properly, you can use UNION to combine those non-contguous cells to your range, like fluff showed above.
 
Upvote 0
How about
VBA Code:
Set LabelRange = Union(TPsheet.Cells(rownumber, "C"), TPsheet.Cells(rownumber, "E"), TPsheet.Cells(rownumber, "G"), TPsheet.Cells(rownumber, "I"))
This is what I needed... Dont know why I couldnft figure this out myself. THANK YOU!! Works like a charm.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Glad we could help & thanks for the feedback.
Nevermind :(
I still get "Application Defined or Object Defined Error" even on this line... Whats mysterious is that it works every 2nd time and throws an error every other 2nd time..
Im gonna go crazy haha
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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