How to get the excel file name / path in VBA ? Help

punnipah

Board Regular
Joined
Nov 3, 2021
Messages
134
Office Version
  1. 2019
Platform
  1. Windows
Hi,

i want to get the excel file name in my path how can i do it ?


Sub Main()
Call initial
' =============================================================================================================================
Dim newDate: newDate = Format(DateAdd("M", 0, Now), "MMMM")

Call wbSetOpen(g_wbMMT, g_dirInputPath, "**.XLSX*")
Call wbSaveReplace(g_wbMMT, g_dirOutputPath + "Output_(I want to get name of file in g_dirInputPath)" & CStr(Format(DateAdd("M", 0, Now), "MMMM_YYYY")) & ".xlsx", False)

1675758447004.png



1675758404268.png
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
g_dirInputPath is a variable and you have not shown enough information for us to answer your question. Is this a full path and you just want to pull out the file name at the end of the path?

Where is g_dirInputPath declared?
Where and how is the value of g_dirInputPath assigned?
What is the value of g_dirInputPath at the point you have highlighted?
 
Upvote 0
g_dirInputPath is a variable and you have not shown enough information for us to answer your question. Is this a full path and you just want to pull out the file name at the end of the path?

Where is g_dirInputPath declared?
Where and how is the value of g_dirInputPath assigned?
What is the value of g_dirInputPath at the point you have highlighted?
d_dirInputPath is :HRD_MMY.XLSX
i want to get this Name to Save A New File On outputpath
 
Upvote 0
if your sub wbsetopen actuallly opens the file you want to use you can find the path and file name very simply, try this code:
VBA Code:
Sub test()
tt = ActiveWorkbook.Path
MsgBox tt
tn = ActiveWorkbook.Name
MsgBox tn


End Sub
 
Upvote 1
d_dirInputPath is :HRD_MMY.XLSX
Is that a typo? Because the code refers to g_dirInputPath

The ":" character is illegal in a Windows file name. What is that doing in there? I think you just need to remove that and append it to g_dirOutputPath. But same problem--you haven't told us what that is. Does it end in a "\" character? If so do this:
Rich (BB code):
Sub Main()
Call initial
' =============================================================================================================================
Dim newDate: newDate = Format(DateAdd("M", 0, Now), "MMMM")

Call wbSetOpen(g_wbMMT, g_dirInputPath, "**.XLSX*")
Call wbSaveReplace(g_wbMMT, g_dirOutputPath & Replace(g_dirInputPath, ":", "") & CStr(Format(DateAdd("M", 0, Now), "MMMM_YYYY")) & ".xlsx", False)
If not do this
Rich (BB code):
Call wbSaveReplace(g_wbMMT, g_dirOutputPath & "\" & Replace(g_dirInputPath, ":", "") & CStr(Format(DateAdd("M", 0, Now), "MMMM_YYYY")) & ".xlsx", False)
 
Upvote 0
Solution
It is separator on a MAC though.
I saw that and thought he was on a Mac, but then saw Windows in the profile and so thought he was on Windows, and then just ended up thinking WTF.

I do not have a Mac but my understanding is that ":" is the separator for the older OS and currently it's now "\".
 
Upvote 0

Forum statistics

Threads
1,215,882
Messages
6,127,532
Members
449,385
Latest member
KMGLarson

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