vba questions
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
vba questions
Hello,
I have a few vba questions if I may pick loungers brains.......
1. When writing vba code, I don't like to specify (full) workbook paths. This is for two reasons, using the same code in live and test regions, and also if folder structures slightly change. Therefore I use code like:
strPath1 = ThisWorkbook.Path & "\"
strPath2 = ThisWorkbook.Path & "\" & "Day" & "\"
....etc
Is it possible, from "ThisWorkbook.Path" to obtain the folder 'back a level'? IOW, if "ThisWorkbook.Path" = P:\1\2\3, how do I get to P\1\2?
2. How can I get the file count in a specific folder?
3. How to copy all files from one folder to another, and prefix each file name with a date?
Thanks for any suggestions.
I have a few vba questions if I may pick loungers brains.......
1. When writing vba code, I don't like to specify (full) workbook paths. This is for two reasons, using the same code in live and test regions, and also if folder structures slightly change. Therefore I use code like:
strPath1 = ThisWorkbook.Path & "\"
strPath2 = ThisWorkbook.Path & "\" & "Day" & "\"
....etc
Is it possible, from "ThisWorkbook.Path" to obtain the folder 'back a level'? IOW, if "ThisWorkbook.Path" = P:\1\2\3, how do I get to P\1\2?
2. How can I get the file count in a specific folder?
3. How to copy all files from one folder to another, and prefix each file name with a date?
Thanks for any suggestions.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba questions
1. Use code like this:
strParent will contain the path of the parent folder of the folder containing the workbook with the code.
2. Use code like this:
lngCount will contain the number of files in the folder specified in strPath.
3. Use code like this:
You can modify this to suit your needs, of course.
Code: Select all
Dim fso As Object
Dim fld As Object
Dim strParent As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(ThisWorkbook.Path)
strParent = fld.ParentFolder.Path
2. Use code like this:
Code: Select all
Dim fso As Object
Dim fld As Object
Dim strPath As String
Dim lngCount As Long
strPath = "..."
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)
lngCount = fld.Files.Count
3. Use code like this:
Code: Select all
Dim fso As Object
Dim fld As Object
Dim fil As Object
Dim strSourcePath As String
Dim strTargetPath As String
strSourcePath = "..."
strTargetPath = "..."
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strSourcePath)
For Each fil In fld.Files
fil.Copy Destination:=strTargetPath & "\" & Format(Date, "yyyymmdd") & fil.Name
Next fil
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: vba questions
Agreed...Brilliant Hans.
Now my questions:
1. What does the ellipse mean in this line of code? >>> strSourcePath = "..."
2. >>> CreateObject("Scripting.FileSystemObject") - Does this allow one to collect info from the system outside of the Excel Application Object?
And here I thought the only way to get a list of files from a directory (or count the amount of files in a folder) was using the DIR function in VBA.
Now my questions:
1. What does the ellipse mean in this line of code? >>> strSourcePath = "..."
2. >>> CreateObject("Scripting.FileSystemObject") - Does this allow one to collect info from the system outside of the Excel Application Object?
And here I thought the only way to get a list of files from a directory (or count the amount of files in a folder) was using the DIR function in VBA.
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba questions
1. The ... should be replaced with the appropriate path. I used the ellipsis because I don't know which path Nathan wants to use.
2. You can use CreateObject to use other object libraries than the standard ones. I'm using late binding here, so that it's not necessary to set a reference to the Microsoft Scripting Runtime library. See this post in the Windows Secrets Lounge for an explanation of the difference between early binding and late binding.
2. You can use CreateObject to use other object libraries than the standard ones. I'm using late binding here, so that it's not necessary to set a reference to the Microsoft Scripting Runtime library. See this post in the Windows Secrets Lounge for an explanation of the difference between early binding and late binding.
Best wishes,
Hans
Hans
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: vba questions
Tx Slinky
Like Don and Alan said...great explanation...(but also far above what I have been exposed to and probably will need to know). Great to learn though!
Like Don and Alan said...great explanation...(but also far above what I have been exposed to and probably will need to know). Great to learn though!
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: vba questions
Another one, slightly modified....
Can anyone see why I am getting a 'RTE 424 - Object required' error on the last but one line?
Code: Select all
Dim arrFiles As Variant
Dim varFile As Variant
Dim strPathT As String, strPath4 As String
Dim folY As String, folM As String, folD As String
folY = "2010"
folM = "3. Mar 2010"
folD = "26 Mar 2010"
strPathT = strParnt & "\" & "Files" & "\"
strPath4 = strParnt & "\" & "Daily Files" & "\" & folY & "\" & folM & "\" & folD & "\" & "Reports" & "\"
arrFiles = Array( _
strPathT & "XYZ4257 TEMPLATE.xls", _
strPathT & "XYZ4258 TEMPLATE.xls", _
strPathT & "XYZ4259 TEMPLATE.xls")
For Each varFile In arrFiles
FileCopy varFile, strPath4 & (Left(varFile.Name, 8) & Format(folD, "dd-mm-yy") & ".xls")
Next varFile
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba questions
varFile is the filename, so you shouldn't use varFile.Name. Simply use varFile instead.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: vba questions
Thanks Hans, but now I am getting 'RTE 52 - Bad file name or number'.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba questions
varFile is the full name, including the path. So you're repeating the path by using strPath4 and Left(varFile, 8).
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: vba questions
Ok, I understand what you mean, but cannot see how to correct it?
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: vba questions
Hans, Thankyou for your help.HansV wrote: 3. Use code like this:
You can modify this to suit your needs, of course.Code: Select all
Dim fso As Object Dim fld As Object Dim fil As Object Dim strSourcePath As String Dim strTargetPath As String strSourcePath = "..." strTargetPath = "..." Set fso = CreateObject("Scripting.FileSystemObject") Set fld = fso.GetFolder(strSourcePath) For Each fil In fld.Files fil.Copy Destination:=strTargetPath & "\" & Format(Date, "yyyymmdd") & fil.Name Next fil
How can I adjust this so that the date becomes a suffix? I'm having difficulty working out how to lose the file extention?
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba questions
You could add the following declarations at the beginning:
Dim strFilename As String
Dim strExtension As String
Dim intPos As Integer
Dim strNewName As String
Change the code within the For ... Next loop to
' Get the filename
strFilename = fil.Name
' Get position of last . in name
intPos = InStrRev(strFilename, ".")
' Filename without extension
strFilename = Left(strFilename, intPos - 1)
' Extension, including the .
strExtension = Mid(strFilename, intPos)
' Assemble new name
strNewName = strTargetPath & "\" & strFilename & Format(Date, "yyyymmdd") & strExtension
' Copy the file
fil.Copy Destination:=strNewName
Dim strFilename As String
Dim strExtension As String
Dim intPos As Integer
Dim strNewName As String
Change the code within the For ... Next loop to
' Get the filename
strFilename = fil.Name
' Get position of last . in name
intPos = InStrRev(strFilename, ".")
' Filename without extension
strFilename = Left(strFilename, intPos - 1)
' Extension, including the .
strExtension = Mid(strFilename, intPos)
' Assemble new name
strNewName = strTargetPath & "\" & strFilename & Format(Date, "yyyymmdd") & strExtension
' Copy the file
fil.Copy Destination:=strNewName
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: vba questions
Sorry, that's what you get if you write air code. Two instructions have to be switched, change
' Filename without extension
strFilename = Left(strFilename, intPos - 1)
' Extension, including the .
strExtension = Mid(strFilename, intPos)
to
' Extension, including the .
strExtension = Mid(strFilename, intPos)
' Filename without extension
strFilename = Left(strFilename, intPos - 1)
' Filename without extension
strFilename = Left(strFilename, intPos - 1)
' Extension, including the .
strExtension = Mid(strFilename, intPos)
to
' Extension, including the .
strExtension = Mid(strFilename, intPos)
' Filename without extension
strFilename = Left(strFilename, intPos - 1)
Best wishes,
Hans
Hans