Setting a range equal to a group of variables

grexcelman

Board Regular
Joined
Mar 1, 2015
Messages
50
Not sure where my syntax is wrong here. I've been trying to concat a few variables into a range based off the location in a shared folder for my coworkers to use when updating a sheet. Hopefully someone can point me in the right direction. When I've checked each variable by using msgbox within the function, each one checked out ok until I get to the range varaible and that's where the function stops and I get a #Value! error with no msgbox popup.

Code:
function test()
Dim location As String
Dim monthly_file As String
Dim function_tb As String
Dim linkfile As Range

location = "My drive address"
monthly_file = "My current month"
function_tb = "charity'!A10:X25"
Set linkfile = Range(location & monthly_file & function_tb)
msgbox linkfile
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Have you tried adding a msgbox just before the "Set linkfile = ..." line

Code:
MsgBox location & monthly_file & function_tb

to see what you are actually trying to set the range to? Might be missing a space or some punctuation?
 
Last edited:
Upvote 0
Have you tried adding a msgbox just before the "Set linkfile = ..." line

Code:
MsgBox location & monthly_file & function_tb

to see what you are actually trying to set the range to? Might be missing a space or some punctuation?


I've tried that, and that works for me, I can verify each string. but it all falls apart when i try to create a range out of it. I'm ultimately trying to plug this concatenated range into a vlookup function but i can't get past this range situation.
 
Upvote 0
I think the issue is maybe my syntax is incorrect for creating a range that has been concatenated from strings that link to another workbook on my drive. the punctuation looks alright but thus far no luck in my trouble shooting.
 
Upvote 0
You can't assign a range variable to a range in a closed workbook.
 
Upvote 0
Post the code and the contents of all relevant variables when the code fails.
 
Upvote 0
Post the code and the contents of all relevant variables when the code fails.

Provided here:
Code:
Function grabmonthly(exp As String, functionTitle As String, BD As String) As LongDim function_tb as String
Dim morun As String
Dim functionname As String
Dim linkfile As Range

If functionTitle = "Marketing" Then
    functionname = "Marketing Info"

'location of the month selection
morun = Range("B7").Value

function_tb = "'[Monthly Trend _ " & morun & " Reporting_" & BD & ".xlsx]" & functionname & " Monthly Trend GL'!$A$12:$BJ$63"
 
Set linkfile = Range(function_tb)
variable contents:
function_tb will read: "'[Monthly Trend _ February Reporting_BD6.xlsx]Marketing Monthly Trend"
morun will read: "February"
BD will read: "BD6"
functionname will read "Marketing"
linkfile essentially is the same as function_tb
 
Upvote 0
When you assign a range, you do not (cannot) use the path:

Code:
Set someRange = Workbooks("someWorkbook").Worksheets("someWorksheet").Range("someRangeString")

BTW, if you close the workbook, the range variable will be destroyed; a range variable can never reference a closed workbook.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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