Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Date format As Folder Name

Status
Not open for further replies.

aba104

Technical User
Aug 20, 2002
11
US
Hi,

I am trying to save a group of files created on the same day in the same folder. Since these files are produced daily, I am trying to group these filesunder in the same folder using date as folder name .

The folder was created by using Dir(Path,vbDirectory) function. But what strange is that after this folder is created, it is still taken by the vba that the folder does not exist and it creates the folder over and over again. I am thinking about if vb can not recognize the folder due to the folder name in date format.

Can any one come up any solution please? Thanks a lot.

JY
 
Without seeing your code it would be difficult to offer an answer.

1) you may be using another directory (maybe to open a file) and that is where VB is saying that it's not finding the folder)

2a) you may consider using a global variable (at the beginning of a module/main macro code) to maintain the pathway.

2b) have you consider saving the pathway with savesetting and upon program termination use the deletesetting commands?

3) you may have a NON-Static variable within the 'Saving' code that gets erased.

4) you may ALREADY be within that folder...you may need to move back to the parent directory or start from the root.


Could be anything. Post your code and let us offer more definitive answers.

--MiggyD
 
Miggy,

Thank you for your help. Here is my code:

mycount = Sheets("Head Sheet").Range("O51").Value

a = Format(Now, "mmddyy")

Set fs = CreateObject("Scripting.FileSystemObject")

If fs.FolderExists(a) = False Then
MkDir a
ChDir a
mycount = mycount + 1
ThisWorkbook.SaveCopyAs Filename:="Teller103-" & mycount & ".xls"
Else
ChDir a
mycount = mycount + 1
ThisWorkbook.SaveCopyAs Filename:="Teller103-" & mycount & ".xls"

End If
Sheets("Head Sheet").Range("O51").Value = mycount

I will try your suggestions. Thank you.

JY
 
You need the complete path of the folder before you can create or even add a file to it.

Try setting the "a" variable to the following and then try a test run:

a = "C:\My Documents\" & Format(Now, "mmddyy")

Where "C:\My Documents\" is the path where the folder is to be located.

Be sure to have the "a" variable "Dim"ed as a String too! ;-)

I hope this helps!


Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Check out my FAQ: faq707-4116 - File and Folder Procedures

It might help you as well! [thumbsup2]


Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
I, too, have to agree with Bowers74 about the specified pathway. You should follow that advise.

Sorry it took so long to respond but at first glance (and I mean glanced) your code appeared to be viral in nature--which I adamantly abhor. But, I hope I'm wrong AND hope this line will fix your delima.


'---Start of code
mycount = Sheets("Head Sheet").Range("O51").Value
a = Format(Now, "mmddyy")
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists(a) = False Then
MkDir a
ChDir a
mycount = mycount + 1
ThisWorkbook.SaveCopyAs Filename:="Teller103-" & mycount & ".xls"
Else
ChDir a
mycount = mycount + 1
ThisWorkbook.SaveCopyAs Filename:="Teller103-" & mycount & ".xls"
End If
[red]ChDir ".."[/red]
Sheets("Head Sheet").Range("O51").Value = mycount
'---End of code


Again, this is a quick fix. You app will need to be started from the same parent directory every time. Otherwise it will reach the root and you may get an error.

--MiggyD
 
Thank you all..

I took Miggy's method, changed directory to "..", it was simple and worked perfectly. Again, thank you all very much!

JY
 
You're welcome.

Just remember that you are constantly creating NEW folders/files...eventually that directory will fill up (the virus-like activity I mentioned above) and you'll have to either delete really old ones OR compress and archive them elsewhere.

Good Luck,
--MiggyD
 
Thank you, Miggy. I will have the files deleted after it accumulates 1 month.

JY
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top