Merge multiple workbooks in one (new) master workbook using VBA - vba

I need some help on integrating multiple workbooks into a single master workbook. I am trying to use the following macro code. Problem is that it doesn't enter the do while loop. I have verified the path too. Kindle help
Sub GetSheets()
Path = "C:\Users\ssehgal\Documents\Excel-Files-For-Macro"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub

Replace this:
Path = "C:\Users\ssehgal\Documents\Excel-Files-For-Macro"
Filename = Dir(Path & "*.xls")
with this:
Path = "C:\Users\ssehgal\Documents\Excel-Files-For-Macro\"
Filename = Dir(Path & "*.xls")
You were missing the \

Related

Converting from IQy to XLSX with VBA

I have a about 40 files that are IQy files that I can open with Excel and I'm trying to go through all of them and save them as xlsx files. What I have so far in VBA is this
Sub ConvertFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "C:\Users\CHI\Downloads"
Filename = Dir(Pathname & ".iqy")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
wb.SaveAs Pathname & Filename & ".xlsx"
wb.Close
Filename = Dir()
Loop
End Sub
To my understanding this loops through my download file where the iqy files are stored and then saveas in xlsx format. When I run it nothing happens.
UPDATE
Sub ConvertFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "C:\Users\CHI\Downloads\"
Filename = Dir(Pathname & "*.iqy")
Application.DisplayAlerts = False
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
wb.SaveAs Pathname & Filename & ".xlsx", FileFormat:=51
wb.Close
Filename = Dir()
Loop
End Sub
This is what worked for me, the only problem I have now is after it changes every file I get a prompt to import data and all I have to press is ok. Is there a way to automate this part so that I can import the data using the table option.
You need to include a wildcard in order to find your iqy files and your pathname will need an additional folder separator to allow the Open and SaveAs to work:
Sub ConvertFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "C:\Users\CHI86786\Downloads\"
Filename = Dir(Pathname & "*.iqy")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
wb.SaveAs Pathname & Filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook
wb.Close
Filename = Dir()
Loop
End Sub
Lastly, to be sure it saves correctly, set the FileFormat parameter when using SaveAs.

Looping through files and protect worksheet by opening and saving one at a time

I am working on more than 200 excel files and I need to protect my excel worksheets before using it to send to other companies. The code below managed to work but the problem is it keeps opening all the excel files in the folder until it ran out of memory to process and I also have to manually save all the files that are opened
Is there a way where the code can loop the excel files in the folder one at a time and save it before proceeding with the next excel file ?
Sub LoopThroughFiles()
FolderName = "C:\Users\Desktop\PROJECTS\PROJECT FILE\A"
If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
Fname = Dir(FolderName & "*.xlsx")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(FolderName & Fname)
' here comes the code for the operations on every file the code finds
ActiveSheet.Protect "password", True, True
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub
Sub LoopThroughFiles()
FolderName = "C:\Users\Desktop\PROJECTS\PROJECT FILE\A"
If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
Fname = Dir(FolderName & "*.xlsx")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(FolderName & Fname)
' here comes the code for the operations on every file the code finds
ActiveSheet.Protect "password", True, True
.save
.close
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub

Find xls files created by excel macro in a folder

The macro that I intend to write must process given csv files in a folder followed by conversion to xls format.
Then I want to find all the created xls files in the same folder and merge them into one xlsx file.
The first loop runs fine (to find all csv files in the folder, process them and convert them into xls format).
However, when I try to find all the xls files, it returns empty. I also tried to debug and add watches. It returns empty string.
I am not sure what is wrong in my code. Please see below.
Sub Macro2()
fname = "Consolidated Excel Spreadsheet" & ".xlsx"
fpath = "C:\Path\"
StrDstFile = fpath & fname
Set objWorkbook = objExcel.Workbooks.Add()
ActiveWorkbook.SaveAs StrDstFile, FileFormat:=51
Set DstWb = ActiveWorkbook
CsvFile = Dir(fpath & "*.csv")
' This runs absolutely fine.
Do While CsvFile <> ""
StrSrcFile = fpath & CsvFile
Set SrcWb = Workbooks.Open(StrSrcFile)
SrcWb.Activate
ActiveWorkbook.SaveAs Replace(SrcWb.FullName, ".csv", ".xls"), FileFormat:=xlExcel8
SrcWb.Close True
Set SrcWb = Nothing
CsvFile = Dir
Loop
XlsFile = Dir(fpath & ".xls")
' This does not find any xls files in the directory, even though the files exist here
Do While XlsFile <> ""
StrSrcFile = fpath & XlsFile
Set SrcWb = Workbooks.Open(StrSrcFile)
SrcWb.Activate
XlsFile = Dir
Loop
End Sub
You are missing a * in XlsFile = Dir(fpath & ".xls")
It should be XlsFile = Dir(fpath & "*.xls")

bad file error while running single macro in multiple file

while running this code its showing bad file name or number
I have stored all my files in "\C:\Users\20098323\Desktop\EXCL\"
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = ActiveWorkbook.Path & "\C:\Users\20098323\Desktop\EXCL\"
Filename = Dir(Pathname & "*.xlsx")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
DoWork wb
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
Sub DoWork(wb As Workbook)
With wb
'Do your work here
.Worksheets(1).Range("A1").Value = "Hello World!"
End With
End Sub
Remove ActiveWorkbook.Path & and the first \ from the line Pathname = ActiveWorkbook.Path & "\C:\Users\20098323\Desktop\EXCL\"
Pathname = "C:\Users\20098323\Desktop\EXCL\"
ActiveWorkbook.Path & "\C:\Users\20098323\Desktop\EXCL\"
This is your problem. It appends the path to the EXCL folder to the path of your current workbook. So you will end up with something like C:\wherever\you\have\your\workbook\C:\Users\20098323\Desktop\EXCL\. Instead use
Pathname = "C:\Users\20098323\Desktop\EXCL\"
Here's a tip for the future: If it gives you an error, press the debug button and it will show you the line where the error occurred. Then you can hover over the variable names and see their current value.

Excel VBA Convert .csv to Excel File

I have a folder which has .csv files, .xls files, and xlsx files. The below code is a portion of an overall project (when I remove the below code, the remaining code achieves what I want). A large chunk of the code was compiled from somewhere (here and around the internet). What I want the code to do is open only the .csv files in the folder, convert them to an Excel file, close the files, and then delete the .csv files in the folder. What ends up happening with the code is that one or both of the files created by the code are deleted from the folder, and I am left with nothing. Thanks in advance for any help.
Sub Test()
'
' Test Macro
'
'Set variables for the below loop
Dim MyFolder As String
Dim MyFile As String
Dim GetBook As String
Dim GetBook2 As String
Dim MyCSVFile As String
Dim KillFile As String
MyFolder = "REDACTED"
MyFile = Dir(MyFolder & "\*.xls")
MyCSVFile = Dir(MyFolder & "\*.csv")
'Open all of the .csv files in the folder and convert to .xls
Do While MyCSVFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyCSVFile
GetBook = ActiveWorkbook.Name
GetBook2 = Left(GetBook, Len(GetBook) - 4)
ActiveSheet.Name = "Sheet1"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=GetBook2, FileFormat:=56
ActiveWorkbook.Close False
Kill MyFolder & "\" & GetBook
Loop
End Sub
You are not calling the Dir function to get the next file.
Sub Test()
'Set variables for the below loop
Dim myFolder As String
Dim getBook As String
Dim myCSVFile As String
Application.DisplayAlerts = False
myFolder = Environ("TEMP") & Chr(92) & "REDACTED"
myCSVFile = Dir(myFolder & "\*.csv")
Do While myCSVFile <> ""
Workbooks.Open Filename:=myFolder & "\" & myCSVFile
getBook = ActiveSheet.Name '<~ Sheet1 of an opened CSV is the name of the CSV
ActiveSheet.Name = "Sheet1"
ActiveWorkbook.SaveAs Filename:=myFolder & Chr(92) & getBook, FileFormat:=56
ActiveWorkbook.Close False
Kill myFolder & Chr(92) & myCSVFile '<~~ delete the CSV, not the workbook
myCSVFile = Dir '<~~ this is important to get the next file in the folder listing
Loop
End Sub
The only worksheet in an opened CSV is named for the CSV (without the .CSV extension) so that can be used in the Workbook.SaveAs method. I've used xlOpenXMLWorkbook as the SaveAs FileFormat type.