VBA for referencing ranged variable name

helpneeded2

Board Regular
Joined
Jun 25, 2021
Messages
110
Office Version
  1. 365
Platform
  1. Windows
I have very little understanding of VBA and am trying to modify some existing code.

The cell in C9 of my sheet contains a file name reference.

The current code has 3 references to C9:

path_ = ThisWorkbook.Worksheets("Settings").Range("C9").Value
ThisWorkbook.Worksheets("Settings").Range("C9").Value = 0
ThisWorkbook.Worksheets("Settings").Range("C9").Value = .SelectedItems(1)

Ideally I just want to set C9 to a variable, so that I only have to change the reference in 1 area if needed.

I have tried a couple different ways, but cannot get it to work due to my limited understanding of VBA.

Could someone please advise if this can be done, and how?

This was what I last tried that did not work:

Dim fileLocation As Range
fileLocation = ThisWorkbook.Sheets("Settings").Range("C9").Range

path_ = ThisWorkbook.Worksheets("Settings").fileLocation.Value






This results in an error
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Ranges are objects so a 'set' statement is required for variable assignment. It works like this

VBA Code:
    Dim fileLocation As Range
    
    Set fileLocation = ThisWorkbook.Sheets("Settings").Range("C9").Range
    
    path_ = fileLocation.Value
    fileLocation.Value = 0
    fileLocation.Value = MyFileDialog.SelectedItems(1)
 
Upvote 0
Solution
This was what I last tried that did not work:

Dim fileLocation As Range
fileLocation = ThisWorkbook.Sheets("Settings").Range("C9").Range

fileLocation is a Range variable, which is an Object, so you need to use Set:
VBA Code:
Dim fileLocation As Range
Set fileLocation = ThisWorkbook.Worksheets("Settings").Range("C9")
Note - you don't need the .Range at the end.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,974
Members
449,095
Latest member
Mr Hughes

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