I want to read certain excel files from a directory and then open them in excel-2007 with VBA.
Here is an example:
directory: c:\temp
file pattern: is xxxxx0123.xls (xxxxx represents the file names).
I try to use Application.FileSearch, but it won't work in Excel 2007. Does any one have good suggestions?
Thanks in advance
You can use DIR to find files matching your pattern, ie this code opens these files, grabs their path, and closes the files again
The code can be made recursive if you need to look in sub-folders
Sub GetFiles()
Dim strFolder As String
Dim strFileName As String
Dim wb As Workbook
strFolder = "C:\temp"
strFileName = Dir(strFolder & "\*123.xls")
Do While Len(strFileName) > 0
Set wb = Workbooks.Open(strFileName)
Debug.Print wb.FullName
wb.Close False
strFileName = Dir
Loop
End Sub
Related
I have a Macro that Loops through Folder where xlsm file is and finds the File based on a condition to match the given part of the file with the file in Folder and skip it. The file full name is 03_2020_STR_BB_2080.xls. Code works perfectly, but my problem starts when I introduce the correct number in this case. 2080 as shown below. When I click on Run button to run the code the Macro doesn't start at all, but when I insert a False value then instead of 2080 any other number or text that is not a part of a file when I click on Run then Macro starts to Run.
Can anybody help me, tnx in advance.
In my folder are three files
03_2020_STR_BB_2080.xls
03_2020_STR_BB_7080.xls
03_2020_STR_BB_2130.xls
I am using following part of a code.
Dim MyFile As String
Dim Filepath As String
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Fname As String
Filepath = ThisWorkbook.Path & "\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "BB2.xlsm" Then
Exit Sub
End If
If MyFile Like "*STR_BB_2080.xls" Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
code continues....
Check by replacing the if condition with this line:
If MyFile Like "*STR_BB_2080*" Then
So, I have this code that I found here: https://stackoverflow.com/a/10382861
And when I first found it, I modified it to actually open the files that it found:
Sub LoopThroughFiles()
Dim SourceFolder As String
SourceFolder = "C:\Users\Jeff\Downloads\IO\"
Dim StrFile As String
StrFile = Dir(SourceFolder & "*.xls")
Dim wb As Workbook
Do While Len(StrFile) > 0
Debug.Print StrFile
Set wb = Workbooks.Open(Filename:=StrFile)
StrFile = Dir
Loop
End Sub
When I first modified this code, it worked perfectly, and would open my files for me.
So I decided to do a bit of house keeping, and cleaned up the code a bit, removed extra spaces etc, however now when I run this exact same code, it now tells me:
Runtime error "1004"
Cannot find file "Excelfile.xls"
I've not moved any files, I've not changed any paths, I've not renamed any files. Everything is exactly the same ... and yet it cannot find the files. I've also tested hard coding the path, with the same error.
One thing I noticed that was interesting, is even if I go back to the bare bones code:
Sub LoopThroughFiles()
Dim StrFile As String
StrFile = Dir("C:\Users\Jeff\Downloads\IO\*.xls")
Do While Len(StrFile) > 0
Debug.Print StrFile
StrFile = Dir
Loop
End Sub
and simply add a Debug.Print Dir, right after the Debug.Print StrFile. It seems that the StrFile and Dir are reading the wrong file at any given time.
First print out is "file1.xls" and the second print out is "file2.xls"
Just not sure if this has anything to do with it.
The filename returned by Dir does not include the source folder. Unless you can reliably use the default folder as the source folder then you need to concatenate SourceFolder onto the StrFile or risk looking in the wrong folder.
Sub LoopThroughFiles()
Dim SourceFolder As String
SourceFolder = "C:\Users\Jeff\Downloads\IO\"
Dim StrFile As String
StrFile = Dir(SourceFolder & "*.xls")
Dim wb As Workbook
Do While Len(StrFile) > 0
Debug.Print StrFile
Set wb = Workbooks.Open(Filename:=SourceFolder & StrFile)
StrFile = Dir
Loop
End Sub
Since I am very new to the excel macro I am trying to develop a code which is able to open the PDF file.But There are some PDF files in my system which are generated by another system therefore those files names change day by day and some figures are included too.
As an example,"Process Report 151120183569844" like this.These figures change everyday.I tried it with adding WILDCARD option but it doesn't work.How do I open this PDF with only a part of the file name?
Sub Open_PDF()
Dim pdfPath As String
pdfPath ="D:\Reports\Process Report*" & ".pdf" 'I used wildcard instead "Process Report 151120183569844"'
Call OpenAnyFile(pdfPath)
End Sub
Function openAnyFile(strPath As String)
Set objShell = CreateObject("Shell.Application")
objShell.Open(strPath)
End Function
As pointed out in another answer, the Dir function with a wildcard should do the trick.
Here's an example using the original openAnyFile function.
Sub Open_PDF()
Dim filePath As String, fileName As String
filePath = "D:\Reports\"
fileName = Dir(filePath & "Process Report*.pdf")
If fileName <> "" Then
openAnyFile filePath & fileName
End If
End Sub
Function openAnyFile(strPath As String)
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
objShell.Open (strPath)
End Function
You cannot open a file using a wildcard - it's just common sense, what if more than one file was matching your criteria - which one would you want to program to open? You have to specify the exact file name to open it.
if there is just one file in the target directory, you can use something like the following code to open it, regardless of its name:
sFound = Dir(ActiveWorkbook.Path & "\Process Report*.xlsm")
If sFound <> "" Then
Workbooks.Open filename:= ActiveWorkbook.Path & "\" & sFound
End If
I have 90 Excel sheets in a folder: each Excel file has a unique name (company number) and contains only one worksheet. However the sheet name is generically named to 'Sheet1' in all files. Is there a VBA code that can rename all these sheets in this folder to their respective file name, minus the '.xlsx'?
Basically I want to combine all sheets to one file (I already have that VBA script). However before I can proceed with that, I have to rename all excel sheet names to their unique identifier (which is the file name).
I already looked online, but didn't see this script yet or saw this script for similar other purposes. Thanks in advance!
I found a solution online, credits to user VoG on https://www.mrexcel.com/forum/excel-questions/660913-vba-code-bulk-rename-first-worksheet-dependent-workbook-name.html
This is the solution. Change MyFolder = "C:\example" to respective folder
Sub RenSheets()
Dim MyFolder As String
Dim MyFile As String
Dim wbname As String
MyFolder = "C:\example"
MyFile = Dir(MyFolder & "\*.xls")
Application.ScreenUpdating = False
Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile
With ActiveWorkbook
wbname = Left(.Name, InStr(.Name, ".") - 1)
.Sheets(1).Name = wbname
.Close savechanges:=True
End With
MyFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
I'm using the following code to open multiple xml files, however they are opening as a read only workbook, however I require it to open as an XML table, any suggestions?
Code:
Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "C:\Documents and Settings\"
ChDir MyPath
TheFile = Dir("*.xml")
Do While TheFile <> ""
'Call Logs 'This calls for Macro2 to run
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
MsgBox wb.FullName
'wb.Close
TheFile = Dir
Loop
End Sub
You need to use Workbooks.OpenXML instead
Set wb = Workbooks.OpenXML(Filename:=MyPath & "\" & TheFile, LoadOption:=xlXmlLoadImportToList)
I'm not exactly which LoadOption you want to use, but you can choose from:
xlXmlLoadImportToList Automatically creates an XML List and imports
data into the list.
xlXmlLoadMapXml Loads the XML file into the XML Source task pane.
xlXmlLoadOpenXml Open XML files in the same way that Excel 2002
opens XML files (for backwards compatibility only).
xlXmlLoadPromptUser Prompts the user and lets them choose the Import
method.