Can variables be used to select a fully qualified range?

MPastor

Board Regular
Joined
Apr 8, 2004
Messages
136
Is it possible to "build" VBA commands using variables (to avoid hardcoding)? If so, how do I actually "execute" them once the command is constructed?

I'm trying to use a set of UserForm defined variables for WORKBOOKS, SHEETS, etc. to select a range using a fully qualified reference with Workbooks(vWorkbookName).Sheets(vWorksheetName).Range(vRangeName), but I'm having a problem actually getting it "select" the range when the code is executed.

I think it may be because the variables for Workbook, Sheet and Range need to be enclosed in quotations, but I'm not sure.

I have three variables with the values indicated below:
vWorkbookName = "Coffee Break Server Rotation Tracking Updated.xls"
vSheetName = "Tracking"
vRangeName = "Week_Ending" (this is an actual Named Range)

I'm trying to create logic to execute the following without hardcoding the workbook, sheet and range as these values will change depending on the options selected by the user.
Code:
'Workbooks("Coffee Break Server Rotation Tracking Updated.xls").Worksheets("Tracking").Range("Week_Ending").Select
First I tried:
Code:
    Workbooks(vWorkbookName).vSheetName(vSheetName).Range(vRangeName).Select
But I received the error: Run-time error '438': Object doesn't support this property or method

Then I tried "creating" the same instruction by concatenating text strings and variables, as follows:
Code:
    vWhere = "Workbooks(""" & vRotationControlFileName & """).Worksheets(""" & _
              vRotationControlSheet & """).Range(""Week_Ending"")"
    vWhere.Select
The result of my string concatenation is shown here:
vWhere.jpg


But, I received a COMPILE ERROR: Invalid qualifier with the vWhere highlighted before the ".Select".

My third, and final, attempt was to create a Function that I pass the vWhere variable string to the function. The ultimate goal is to select the desired range and perform a FIND command looking for the desired value. Here's the Function I attempted to use:

Code:
Public Function FindFunction(vWhat, vWhere) As Boolean
'   Returns TRUE if the Search Criteria is found
Dim vFindResults
vFindResults = vbFalse

MsgBox "vWhere: " & vWhere

vWhere.Select

MsgBox "vWhat: " & vWhat

On Error Resume Next
vFindResults = Selection.Find(What:=vWhat, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate

If vFindResults Then
    MsgBox "SUCCESS!!! Desired text found!!!   vFindResults: " & vFindResults
Else
    MsgBox "Week ending date: " & vWhat & " not found in Rotation Control file."
End If

End Function
This time I received the message: Run-time error '424': Object Required
On the statement: vWhere.Select (the line between the two MsgBox statements)

Can someone help me with my approach(es) or suggest another way to accomplish this??
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You were very close with your first attempt. This should work:

Workbooks(vWorkbookName).Worksheets(vSheetName).Range(vRangeName).Select

Object qualifires can be strings or variables containing strings.
 
Upvote 0
Andrew, THANK YOU!! When I read your reply, I realized I had a typo in my original post in the syntax for my first attempt: Workbooks(vWorkbookName).vSheetName(vSheetName).Range(vRangeName).Select

with the .vSheetname(vSheetname), however your one-liner definitely is working and now I'm not sure what my original syntax was. I've been battling what now appears to be a "self-inflicted gremlin" for two days, so THANK YOU!!!
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,553
Members
449,038
Latest member
Guest1337

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