I have multiple Excel files and they all have the same Worksheet in them named Fixture.
I am trying to copy all of them (they are all in the same folder) and paste them in my destination Excel file under the name of the original Excel file.
For example:
[Excel1]Fixture will be a new worksheet in Main.xlsx named Excel1 after running the VBA. Same with Excel2, Excel3, etc.
Please help!
I suggest you act in four steps:
Create a workbook, e.g. "Collect.xlsx" to collect all "Fixture" sheets
Get your Excel file names and loop through them
In the loop, copy each "Fixture" sheet to your Collect.xlsx
Close and save Collect.xlsx
To 2: You can get the Excel file names either by dir() or by fileDialog:
dir:
http://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.90).aspx
You can use it with or without parameter, thus looping through a
directory. Useful if you want a simple loop, or if you have hundreds of files.
FileDialog: Use the famous FileDialog by Karsten Pries. You can select multiple names
and then loop through them. Download: http://www.kpries.de/download/FileDialog.zip
Your loop might look somehow like this:
...
...
fileDialog.Filter1Suffix = "*.xls"
fileDialog.Filter1Text = "Excel Dokumente"
fileDialog.ShowOpen
fileName = fileDialog.fileName
Do While fileName <> ""
ImportOneSheet fileName ' <<< your own method to collect Fixture
fileName = fileDialog.GetNextFile
Loop
...
If you use dir, just set your ImportOneSheet method in the dir loop.
To 3: Some crucial commands are:
Application.Workbooks.Open (fileName)
Set myExcel = GetObject(, "Excel.Application")
Set importWorkBook = Application.ActiveWorkbook
importWorkBook.Sheets("Fixture").range("A:ZZ").Copy
collectorWorkBook.Sheets(consolidationSheetName).range("A:ZZ").PasteSpecial xlPasteValuesAndNumberFormats, xlPasteSpecialOperationNone, True, False
importWorkBook.Close
These are merely snippets to give you ideas. You might want to find out more about these.
To 4: use the saveAs command: xlam.ActiveWorkbook.SaveAs path, xlOpenXMLWorkbook, , , False, False and the close command: xlam.Workbooks.Close.
For anything remaining, you will find tons of snippets in the net. Good luck :-)
Related
I have a folder of .xlsx files, each identical in layout.
I need to write a macro in excel to open each file in turn (100+ files)
then get the data (a name) from a single cell, and drop it in a new excel worksheet, move on to the next and insert that below the last one etc.
Giving me basically a list of names from data not file names)
Here is (pretty much) exactly what you're trying to do. Next time do a little bit of googling before you ask! :)
http://www.excel-easy.com/vba/examples/files-in-a-directory.html
ROUGH CODE UNSURE IF IT WILL WORK: But here is the basic idea of what you need to modify in the example I sent you. If you look at the example again, it does everything you need and then some. Since you weren't interested in all worksheets, you don't have to loop through all worksheets in a workbook. You can just open it up, read your cell of interest, and then close it. The Do While loop will do this for every Excel file in your directory. AGAIN! Please modify this example accordingly before you use it.
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer
Application.ScreenUpdating = False
directory = "c:\test\"
fileName = Dir(directory & "*.xl??")
Do While fileName <> ""
i = i + 1
Workbooks.Open (directory & fileName)
Workbooks("files-in-a-directory.xls").Worksheets(1).Cells(i, 1).Value = Workbooks(fileName).Worksheets(1).Cells(x, y) <-- whatever your cell of interest is
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
I'm very new to vba programming and I would like to know what is the best method to do the following operation : read data from two Excel files (from a particular column of each file) and write this data in a third Excel file, writing the data contained in the first file first then the data contained in the second file. The figure below illustrates what I want to do :
How can I do that on click on a command button, without opening the two first files (I mean I don't want the user to see them).
You need to make so
Workbooks.Open Filename:= "c:\myfile\FILE1.xls"FILE1.xls"
Workbooks.Open Filename:= "c:\myfile\FILE2.xls"
After you open the FILE1.xls and FILE.xls you can create or open FILE3.xls,
in this case we create the FILE3.xls
Dim wb As Workbook
Set wb = Workbooks.Add
After this you read the data in FILE1.xls and FILE2.xls, you can read with WHILE or other LOOP or you read with COPY and PASTE data in new file.
In this case we see the LOOP strategy.
Dim i as Integer
Dim value as String
i=0
value = value = Workbooks("FILE1.xls").Worksheets("sheet1")..Range("B:B").Cells(i, 1)
Workbooks("FILE1.xls").Worksheets("sheet1").Activate
while value <> ""
Workbooks("FILE1.xls").Worksheets(2).Range("A:A").Cells(i, 1) = value
value = value = Workbooks("FILE1.xls").Worksheets("sheet1")..Range("B:B").Cells(i, 1)
Wend
Repeat ste for "FILE2.xls" read data and write in "FILE3.xls"
and at the and you close
wb.SaveAs "c:\myfile\"FILE3.xls""
wb.Close
And close the read data file
Workbooks("FILE1.xls").Close
Workbooks("FILE2.xls").Close
I am a novice at Visual Basic. I can use either Excel 2010 or Excel 2013 for this task.
I have dozens of workbooks with data on the first worksheet of each. For example One.xlsx, Two.xlsx, Three.xlsx, Four.xlsx each contain information on their respective Sheet1.
I need the information on Sheet1 from each workbook to be combined into a single workbook with sheets that are named from the file name of the original workbook. So for example combined.xlsx would have 4 sheets named One, Two, Three, Four. In every case all information on the underlying worksheets should be copied and combined in the new Workbook as shown below.
The Format I need
I found this Macro / Add-In online that gets me close to what I need using the open files add in choice.
http://www.excelbee.com/merge-excel-sheets-2010-2007-2013#close
The Open Files Add-In successfully allows me to aggregate the various Workbook's worksheets into a single workbook. However the tabs are not named from the name of the original file.
Correct aggregation of sheets, but incorrect worksheet names.
For now all the underlying Workbooks will be in the same folder. The ability to browse and select the files would be nice if this ever changes but if that is too difficult, just indicating the directory path in the Visual Basic code would work. As far as the resultant combined output probably ought to be a new workbook, the filename of the new workbook isn't that important. It could be called combined.xlsx for example.
The following accomplishes the task.
Option Explicit
Private Sub CommandButton1_Click()
Dim directory As String, fileName As String, sheet As Worksheet, total As Integer
Dim WrdArray() As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
directory = "c:\test\"
fileName = Dir(directory & "*.xl??")
Do While fileName <> ""
Workbooks.Open (directory & fileName)
WrdArray() = Split(fileName, ".")
For Each sheet In Workbooks(fileName).Worksheets
Workbooks(fileName).ActiveSheet.Name = WrdArray(0)
total = Workbooks("import-sheets.xlsm").Worksheets.Count
Workbooks(fileName).Worksheets(sheet.Name).Copy after:=Workbooks("import-sheets.xlsm").Worksheets(total)
GoTo exitFor:
Next sheet
exitFor:
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
In Excel press Alt+F11, this will open the Excel VBA editor.
Article http://www.excel-spreadsheet.com/vba/debugging.htm explains some basics how to use it.
In Module1 there are 2 short subroutines opensheets and merge containing ~50 lines of code.
Use F1 with cursor within words you don't understand, to learn what it means.
Once you understand what the code does, you can tailor it to your needs.
I need to save several xlsm files as CSV in order to import them in other programs (such as R etc.). Well I've written the necessary import routines in all the other programs and they work very nicely with case 1:
Case 1: Manually saving an xlsm as CSV
If I use this option and manually save each file as CSV and click YES on all prompts, I get a .csv file which looks very similar to a normal Excel file when opened again within excel. It is a standard column view, and nothing comma separated etc. (maybe it is, but it doesn't look that way..)
Case 2: Saving an xlsm from VBA as CSV
Well here I get a completely different file when opened again in Excel. This one looks like a "real" csv file with all values being comma separated.
My questions are:
1. Why is there any difference?
2. How can I programmatically reach Case 2 from VBA? Or is that impossible?
If 2 is impossible I have to rewrite my import code routines to handle the "normal" csv file...not very difficult but still a lot of work and I'm really wondering why there even is a difference..
Q1: I don't think there is a difference, at least not in an example I pulled together. Q2: Try this out:
I've got 3 example XLSM files in C:\stack\folder1 as pictured below:
Each file has a single data sheet, which we'll turn into CSVs:
I'm sure your routine is much more complicated, but to test the CSV output I'm just going to loop through the files and save each as xlCSV:
Option Explicit
Sub TestCSVOutput()
Dim DataBook As Workbook
Dim DataSheet As Worksheet
Dim FilePaths(3) As String
Dim FileIdx As Long
'set up file paths for test
FilePaths(1) = "C:\stack\folder1\test_file_01.xlsm"
FilePaths(2) = "C:\stack\folder1\test_file_02.xlsm"
FilePaths(3) = "C:\stack\folder1\test_file_03.xlsm"
'loop through array and save each file as a CSV
Application.DisplayAlerts = False
For FileIdx = 1 To UBound(FilePaths)
Set DataBook = Workbooks.Open(FilePaths(FileIdx))
Set DataSheet = DataBook.ActiveSheet
DataBook.SaveAs FileFormat:=xlCSV '<~~ the save step
DataBook.Close
Next FileIdx
Application.DisplayAlerts = True
End Sub
Once the script completes, I end up with three CSV files:
Each file is comma-delimited when opened in a text editor:
I am new to Excel macros.
I have a folder having many files.lets say A,B,C
They all have a worksheet named Summary.
I want a new file named Summary
Which iterates through each file and copies the summary worksheet...into separate worksheet in summary file.
The summary file will hv 3 sheets named A,B, C each having its own summary
First, you'll need a list of files. You can use another spreadsheet to hold the list, or you can scan the directory as follows:
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("C:\temp")
s = ""
For Each fil In fld.Files
s = s & fil.Name & vbCrLf 'a sample of doing something with each fil
'here you can open each workbook using Application.Workbooks.Open
'then, or later, if you like you can close the ones you opened
'by remembering which workbooks were opened before you started.
Next
-- See: http://msdn.microsoft.com/en-us/library/aa242702%28v=vs.60%29
Next, you'll need to create a new workbook, which can be done using
Set newWkb = Application.Workbooks.Add()
-- See: http://msdn.microsoft.com/en-us/library/ff840478.aspx
Now you can use the techniques described by the above posters to copy the worksheet from one workbook to the new one.
For reference, here's some good information on the VBA language. Check out the various Functions, Objects, and Methods, etc.. under the Language Reference: http://msdn.microsoft.com/en-us/library/aa242702%28v=vs.60%29
And here's some good information about the Excel object model: http://msdn.microsoft.com/en-us/library/ff194068