VBA Dynamic variable

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
Trying to create a script that will open a directory, select the file and assign the workbook name and worksheet name to variables. The issue is the file in a unique format, below is a sample of the list of files. How do I select the most recent one (the bolded one)?
newfile_2021-03-11
newfile_2021-02-11
newfile_2021-04-11
newfile_2021-05-11

Currently my code is :
Sub OpenWorkbooksAndChangeNames
Workbooks.open("\\datafiles\newfile*xl.xlsx" <- as you can see im trying to figure out how to open the newest file
Dim wb As Workbook
Dim ws As Worksheet

Set wb = DatraFile_Workbook
Set ws = DataFile_Sheet1 <- this is the second part of my code. Assigning the opened newfile_2021-05-11 to a variable (wb) along with the first sheet name (ws)
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Use the VBA function Dir - a VBA help must read ! - to list each file, extract the date of each file name in order to compare and to keep the last one​
but as a trick often the last one listed under this function is the more recent according to your static format 'newfile_YYYY-MM-DD' …​
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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