Pass concatenated string + variables as Range?

Dimitris254

Board Regular
Joined
Apr 25, 2016
Messages
139
I'm having hard time getting this to work:
Code:
Dim lastRow As Long
Dim results_range As Range
Dim column_results As String
Dim wsDATA As Worksheet
Set wsDATA = ThisWorkbook.Worksheets("DATA")

With wsDATA

  Set results_range = .Range(column_results & "2:" & column_results & lastRow) 

End With

the point is that i want a flexible code, where i find the columns and then create the range, rather than having " Set results_range = .Range("K2:K30") " (which works fine btw)

the error i get is:
Code:
run-time error 1004:

Method 'Range' of object '_Worksheet' failed
:(
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Where are you assigning a value to column_results and lastRow?
 
Upvote 0
I'm having hard time getting this to work:
Code:
Dim lastRow As Long
Dim results_range As Range
Dim column_results As String
Dim wsDATA As Worksheet
Set wsDATA = ThisWorkbook.Worksheets("DATA")

With wsDATA

  Set results_range = .Range(column_results & "2:" & column_results & lastRow) 

End With

the point is that i want a flexible code, where i find the columns and then create the range, rather than having " Set results_range = .Range("K2:K30") " (which works fine btw)

the error i get is:
Code:
run-time error 1004:

Method 'Range' of object '_Worksheet' failed
:(
Hi Dimitris254,

What is column_results other than a String? Where does it get this information from?

You are also referencing lastRow but do not have it being defined anywhere in your code?
 
Upvote 0
both column_results and lastRow are defined, just forgot to put the code in, as imho the problem is in concatenating the range.

Rich (BB code):
Rich (BB code):
Dim lastRow As LongDim column_results As String

lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
column_results  = .Cells.Find(What:="results", SearchOrder:=xlByColumns).Column
 
Upvote 0
Column returns a column number, not a letter. That is your problem. So you should be using:
Code:
With wsDATA

  Set results_range = .Range(.Cells(2, column_results), .Cells(lastRow, column_results)) 

End With
 
Upvote 0
cheers, that fixed it, although now i get errors with the SumIfs WorksheetFunction :confused:

Rich (BB code):
SumSheet = Application.WorksheetFunction.sumifs(results_range, _
           test_range, "test A", _
           name_range, "Peter")

the other ranges are defined in the same way inside the With - End With statement. The SumIfs is outside the With - End With (it's the same error if i put it inside):
Rich (BB code):
Error '1004':

Unable to get the SumIfs property of the WorksheetFuction class
 
Upvote 0
Can you show ALL the code where each range is created?

Best guess is that they are not all equal demensions (which is required for sumifs)
 
Upvote 0
[SOLVED] Re: Pass concatenated string + variables as Range?

i love you :biggrin:

the problem was that lastRow i had was a common code for the last non-empty row ( e.g. lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ), which is something like 30k, while i was testing the sub from row 2 to 30.

the reason i don't usually put the whole code is that most subs i'm working on are lengthy, so i cut the part of the code with the problem. Next time i'll put more effort into it.

thanks all again
 
Last edited:
Upvote 0
Re: [SOLVED] Re: Pass concatenated string + variables as Range?

You're welcome.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,107
Members
449,096
Latest member
provoking

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