VBA to open folder with variable name

Freedox

New Member
Joined
Mar 11, 2015
Messages
39
Hi,

Working on a VBA code to allow me open a folder which was created a day before. Path and the partial folder name is static. The one thing that is variable is the end of folder name which would have a random date stamp. Example:

Code:
Reporting_Extracts_14112017_82404

VBA so far:

Code:
Private Sub fldOpen_Click()
Dim Foldername As String, strDate As String
strDate = Format(Now - 1, "ddmmyyyy")
Foldername = "\\pc\Path\Extracts\Reporting_Extracts_" & strDate & "_*"
Shell "C:\WINDOWS\explorer.exe """ & Foldername & "", vbNormalFocus
End Sub

Just can't figure out how to open Reporting_Extracts_14112017_[ANY_DATE_STAMP]....

Anyone had similar issue?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi. You'll need to use Dir() to find a matching folder name:

Code:
Private Sub fldOpen_Click()

Const staticFolder = "\\pc\Path\Extracts\"

Dim Foldername As String, strDate As String

strDate = Format(Now - 1, "ddmmyyyy")

Foldername = Dir(staticFolder & "Reporting_Extracts_" & strDate & "_*", vbDirectory)

Shell "C:\WINDOWS\explorer.exe """ & staticFolder & Foldername & """", vbNormalFocus

End Sub

WBD
 
Upvote 0
Hi. You'll need to use Dir() to find a matching folder name:

Code:
Private Sub fldOpen_Click()

Const staticFolder = "\\pc\Path\Extracts\"

Dim Foldername As String, strDate As String

strDate = Format(Now - 1, "ddmmyyyy")

Foldername = Dir(staticFolder & "Reporting_Extracts_" & strDate & "_*", vbDirectory)

Shell "C:\WINDOWS\explorer.exe """ & staticFolder & Foldername & """", vbNormalFocus

End Sub

WBD


Legend! Worked like a charm! Cheers!
 
Upvote 0
Hi. You'll need to use Dir() to find a matching folder name:

Code:
Private Sub fldOpen_Click()

Const staticFolder = "\\pc\Path\Extracts\"

Dim Foldername As String, strDate As String

strDate = Format(Now - 1, "ddmmyyyy")

Foldername = Dir(staticFolder & "Reporting_Extracts_" & strDate & "_*", vbDirectory)

Shell "C:\WINDOWS\explorer.exe """ & staticFolder & Foldername & """", vbNormalFocus

End Sub

WBD


This code worked perfectly to OPEN a folder... Now I'm trying to modify to actually get a file from this folder and import into the ACCESS db... and again... getting nowhere...

Code:
Const staticFolder = "\\pc\Path\Extracts\"


Dim strPathFile As String, strFile As String, strPath As String, strDate As String
Dim strTable As String
Dim blnHasFieldNames As Boolean


strDate = Format(Now, "ddmmyyyy")


strPath = Dir(staticFolder & "Reporting_Extracts_" & strDate & "_*", vbDirectory)


blnHasFieldNames = True


strTable = "dbo_MV_REP_LEVEL"
strFile = Dir(strPath & "MV_REP_LEVEL.csv")


If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
If Dir(strPath & "*.*") = "" Then
MsgBox "The folder doesn't contain (visible) files"
Else

I can't get this code to see this specific folder... If I set as a static, works fine....
 
Upvote 0
Code:
Const staticFolder = "\\pc\Path\Extracts\"

Dim strPathFile As String, strFile As String, strPath As String, strDate As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

strDate = Format(Now, "ddmmyyyy")
strPath = staticFolder & Dir(staticFolder & "Reporting_Extracts_" & strDate & "_*", vbDirectory)

blnHasFieldNames = True

strTable = "dbo_MV_REP_LEVEL"

If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
strFile = Dir(strPath & "MV_REP_LEVEL.csv")
If Dir(strPath & "*.*") = "" Then
MsgBox "The folder doesn't contain (visible) files"
Else

Something like that.

WBD
 
Upvote 0
Thanks!!! It worked!

However, I have a separate question... Now this code loops through all files in the folder... And I would only need VBA to take "MV_REP_LEVEL.csv" file... I know I have this set up to take one, but am I missing something there?
 
Upvote 0
You only posted partial code. It's hard to say.

WBD

I have the full code below:

Code:
Private Sub cmdImport_Click()

Const staticFolder = "\\pc\Path\Extracts\"


Dim strPathFile As String, strFile As String, strPath As String, strDate As String
Dim strTable As String
Dim blnHasFieldNames As Boolean


strDate = Format(Now, "ddmmyyyy")


strPath = staticFolder & Dir(staticFolder & "Reporting_Extracts_" & strDate & "_*", vbDirectory)


blnHasFieldNames = True


strTable = "dbo_MV_REP_LEVEL"




If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
strFile = Dir(strPath & "MV_REP_LEVEL.csv")
If Dir(strPath & "*.*") = "" Then
MsgBox "The folder doesn't contain (visible) files"
Else


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' If folder IS NOT empty, then PURGE
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


DoCmd.OpenQuery "purgeMV_REP_PUBLISHED_WCID_LEVEL"


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Once purged LOOP file import
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Do While Len(strFile) > 0
strPathFile = strPath & strFile


DoCmd.TransferText acImportDelim, "spec", strTable, strPathFile, blnHasFieldNames




     strFile = Dir()
Loop


    MsgBox "Files Sucessfully Imported"


End If


End Sub
 
Upvote 0
Looks like you need something like this:

Code:
Private Sub cmdImport_Click()

Const staticFolder = "\\pc\Path\Extracts\"

Dim strPathFile As String, strFile As String, strPath As String, strDate As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

strDate = Format(Now, "ddmmyyyy")
strPath = staticFolder & Dir(staticFolder & "Reporting_Extracts_" & strDate & "_*", vbDirectory)
If strPath = "" Then
    MsgBox "Reporting extracts folder for today not found"
    Exit Sub
End If

blnHasFieldNames = True
strTable = "dbo_MV_REP_LEVEL"

If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strFile = Dir(strPath & "MV_REP_LEVEL.csv")
If strFile = "" Then
    MsgBox "The folder doesn't contain MV_REP_LEVEL.csv"
    Exit Sub
End If

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' If folder IS NOT empty, then PURGE
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DoCmd.OpenQuery "purgeMV_REP_PUBLISHED_WCID_LEVEL"

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Once purged LOOP file import
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, "spec", strTable, strPathFile, blnHasFieldNames

MsgBox "File Sucessfully Imported"

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,224,939
Messages
6,181,878
Members
453,068
Latest member
DCD1872

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