Have cell reference as Naming convention

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
125
I need to change how some code looks up a certain directory, previously I had it static where it was just a set folder. However now I want to make it a cell reference on a workbook im just not sure how to do so. Below is what I have, I would like it so its refering to cell A2 on a tab called Renaming, code to change in red below

This is the line of code I need to change which works fine.
Set objFolder = objFSO.GetFolder("D:\Users\d12\Desktop\John\Data Files - Test")

This is the entire piece of code
Sub Listfilenames()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer


'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:\Users\d12\Desktop\John\Data Files - Test")
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'print file name
Cells(i + 1, 1) = objFile.Name
'print file path
'Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
End Sub
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,669
For the current code example,
Rich (BB code):
Set objFolder = objFSO.GetFolder("D:\Users\d12\Desktop\John\Data Files - Test")



what exactly would be in cell A2?
 

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
125
A folder directory that the user could change when they needed to. Its so they can change from month to month without having to change the code each time.

So this month it would be
D:\Users\d12\Desktop\John\Data Files\July 2019
Where as next month it would be
D:\Users\d12\Desktop\John\Data Files\August 2019
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,669
A folder directory that the user could change when they needed to. Its so they can change from month to month without having to change the code each time.

So this month it would be
D:\Users\d12\Desktop\John\Data Files\July 2019
Where as next month it would be
D:\Users\d12\Desktop\John\Data Files\August 2019
Assuming that's the full path to the folder:

Rich (BB code):
Dim fPath as String
fPath = Range("A2").Value
'your code
Set objFolder = objFSO.GetFolder(fPath)
'more code

 

Forum statistics

Threads
1,082,392
Messages
5,365,165
Members
400,827
Latest member
Mitzescu

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top