Have cell reference as Naming convention

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
160
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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?
 
Upvote 0
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
 
Upvote 0
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

 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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