I have a macro here that consolidates all files into a master file. Currently this copies the entire worksheet from each file and stacks it into the master file. This ideal code will go into source file and find the header row and copy below. The header row is not static. Sometimes its on Row 5, sometimes row 15. There is data above the header and its usually long text strings.
How do I edit the below code to do that?
Thanks In Advance!
Here is the Code:
Sub Consolidate_BST()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim sourceRange As Range
Dim destrange As Range
Worksheets("Consolidate BSts").Range("A1:J50000").ClearContents
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = ThisWorkbook.Sheets("Consolidate BSts")
' Modify this folder path to point to the files you want to use. 'M
FolderPath = "C:\Users\413315\Documents\\March Bluesheets"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NRow).Value = FileName
' Modify this range for your workbooks.
' It can span multiple rows.
Set sourceRange = WorkBk.Worksheets("Regional Estimates").Range("B3:J1000")
' Set the destination range to start at column B and
' be the same size as the source range.
Set destrange = SummarySheet.Range("B" & NRow)
Set destrange = destrange.Resize(sourceRange.Rows.Count, _
sourceRange.Columns.Count)
' Copy over the values from the source to the destination.
destrange.Value = sourceRange.Value
' Increase NRow so that we know where to copy data next.
NRow = NRow + destrange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
SummarySheet.Columns.AutoFit
Worksheets("Consolidate BSts").Range("D2:D50000").ClearContents
End Sub
Assuming that the header is always in the same column, you could add something like the following to the top of your code:
Dim cond As Boolean
Dim headerRow As Integer, i As Integer
cond = False
While cond <> True
i = i + 1
If sourceRange.Cells(i, 1) = "Name of Header" Then 'For a header in column A
i = headerRow
cond = True
End If
Wend
Then with you should be able to modify the range of data that it intakes based on what row the headers are in.
Also you could change the if statement to something more general, but it's hard to know what it should be without knowing what your headers and data look like.
Related
I have two Excel files I need to merge into one Excel file based on a primary key. I need to do this in vb.net and have no idea where to start. One file is a data list and the other a matrix. I need the matrix fields added to the data list and depending on the primary key the data rows in the data list will be populated by the corresponding matrix row. I have the following but I am unsure if I'm going in the right direction. If so, then how do i save it as a new Excel file?
Dim DT1 As DataTable
DT1.Rows.Add(DtSet)
Dim DT2 As DataTable
DT2.Rows.Add(DtSet2)
DT1.PrimaryKey = New DataColumn() {DT1.Columns(ComboBox1.SelectedItem)}
DT2.PrimaryKey = New DataColumn() {DT1.Columns(ComboBox2.SelectedItem)}
DT1.Merge(DT2)
If I were you, i would use the Excel consolidate feature for this.
https://www.ablebits.com/office-addins-blog/2015/09/01/consolidate-excel-merge-sheets/#consolidate-data-excel
If you really have to use VB for this, you can try the script below.
Sub Merge2Workbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
' Modify this folder path to point to the files you want to use.
FolderPath = "C:\Users\Peter\invoices\"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NRow).Value = FileName
' Set the source range to be A9 through C9.
' Modify this range for your workbooks.
' It can span multiple rows.
Set SourceRange = WorkBk.Worksheets(1).Range("A9:C9")
' Set the destination range to start at column B and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("B" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
End Sub
You may find this interesting too.
https://siddharthrout.wordpress.com/2012/06/05/vb-netvba-copy-rows-from-multiple-tabs-into-one-sheet-in-excel/
I want to copy values from one workbook and paste them into a master workbook.
The line
Set DestRange = DIAAggregation.Range(1 & NRow)
stops the debugger and gives me the error message:
Method 'Range' of object "_workbook" failed
Upon looking online, I am aware that I am not fully qualifying my range, but I do not see what I can do to fully qualify it.
The code is below, and the line in question is the last line.
Sub DIA_Concatenate()
'
'
'
'
Dim DIAAggregation As Worksheet
Dim DIAMaster As Workbook
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim Month As String
Dim Year As String
' Prompts the user to specify which DIA data
' is being aggregated (Month and Year).
' Useful for checking data source and SaveAs file name.
Month = InputBox("What month's data is being aggregated?")
Year = InputBox("What year's data is being aggregated?")
' Points the macro to the proper data source
' (UPDATE THIS LINE TO YOUR DATA SOURCE!!!)
FolderPath = _
"G:\Analytical Services\General Team Folders\Kyle\DIA Aggregation Bank\"
' Opens the master workbook that is to have data added to it,
' and declares the first sheet for the macro.
Set DIAMaster = Workbooks.Open(FolderPath & "*Aggregation*")
Set DIAAggregation = DIAMaster.Worksheets(1)
' Incrementer to keep track of where new rows should be appended.
NRow = DIAAggregation.Rows.Count + 1
Dim LastRow As Long
' Call Dir the first time,
' pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until all .xl files in the source folder have been read.
Do While FileName <> ""
If InStr(1, FileName, "Aggregation") > 0 Then
FileName = Dir()
GoTo Jump
End If
If InStr(1, FileName, Month) = 0 Then
FileName = Dir()
GoTo Jump
End If
' Open a workbook in the folder.
Set WorkBk = Workbooks.Open(FolderPath & FileName)
Dim J As Integer
' Loop through data sheets to collect data.
For J = 2 To Sheets.Count ' From sheet 2 to last sheet.
' Make the sheet active, find where the data is,
' and select the data.
Sheets(J).Activate
LastRow = WorkBk.Worksheets(J).Cells.Find(What:="*", _
After:=WorkBk.Worksheets(J).Cells.Range("A1"), _
SearchDirection:=xlPrevious, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows).Row
Set SourceRange = WorkBk.Worksheets(J).Range("A3:E" & LastRow)
' Set the destination range to start at column A and
' be the same size as the source range.
Set DestRange = DIAAggregation.Range(1 & NRow)
Per the last comment, changing the declaration of NRow by adding in .UsedRange between the spreadsheet variable and .Rows resolves the issue PartyHatPanda pointed out.
I am working on copying the contents of several CSV files into a single, new CSV file, and am having trouble with my vba code. I am aware of the CMD tool to copy .csv files, but this does not work for me, as my directory is stored on the network and I can't path to it from the CMD window (I get an error about using a UNC address). My boss would prefer for the code to have zero human interaction, so moving the files to a directory on the computer, running the CMD, and then moving the results back is not an option.
Per my boss's request, the code needs to do the following:
"Every time the macro is run, the new master file should be saved over when it's run so the report pulls the same file each time."
A logical consequence of this is that the macro should catch a particular string in the resulting file name and "skip over" that file when making a new version. Also, every .csv file has headings, so my ranges are set up to avoid copying them.
Below is the code I have written thus far. When I try to run the macro, I get a few errors to come up with the line:
Set WorkBk = Workbooks.Open(FolderPath & FileName)
They're always 1004 messages, and they either say my created file is either read-only/encrypted, or they tell me that Method 'Open' of object 'Workbooks' failed.
What do I need to change or do to get the below code to work? I am confident in this code because I slightly modified it from code I wrote yesterday to do a similar task with .xlsx files. Any help is greatly appreciated, thank you.
Sub CSV_Aggregate()
'
'
'
'
Dim CSVAggregation As Worksheet
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
' Points the macro to the proper data source (UPDATE THIS LINE TO YOUR DATA SOURCE!!!)
FolderPath = "\\usilsvr01\lin#mktg\Analytical Services\DIA\Offers Data Question to Exclude"
' Creates a blank workbook to host the aggregation, and names the first worksheet appropriately.
Set CSVAggregation = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Sheets(1).Name = "DIA Aggregation"
' Heads the worksheet with the relevant fields to be aggregated.
CSVAggregation.Range("A1:C1") = Array("Manufacturer Number", "Offer Code", "Data Question")
' Incrementer to keep track of where new rows should be appended.
NRow = 2
Dim LastRow As Long
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.csv")
' Loop until all .csv files in the source folder have been read.
Do While FileName <> ""
' Macro should skip over the previous version of the aggregate file
If InStr(1, FileName, "Aggregate") > 0 Then
FileName = Dir()
End If
' Open a workbook in the folder.
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Loop through data sheets to collect data.
Sheets(1).Activate ' Make the sheet active, find where the data is, and select the data.
LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
After:=WorkBk.Worksheets(1).Cells.Range("A1"), _
SearchDirection:=xlPrevious, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows).Row
Set SourceRange = WorkBk.Worksheets(1).Range("A2:C" & LastRow)
' Set the destination range to start at column A and
' be the same size as the source range.
Set DestRange = DIAAggregation.Range("A" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increment NRow so that data is not overwritten.
NRow = NRow + DestRange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all data is readable.
CSVAggregation.Columns.AutoFit
CSVAggregation.Rows.AutoFit
' Places cursor on the first sell so document doesn't open highlighted or anywhere besides the top.
CSVAggregation.Range("A1").Select
' Creates variable to hold SaveAs name for Aggregation Report.
Dim workbook_Name As String
workbook_Name = "CSV Aggregate"
' Saves the workbook in the folder that the data is found in (BE SURE TO CHECK TAHT YOU HAVE THE FOLDER/FILES WITH WHICH YOU SHOULD BE WORKING!!!!)
ActiveWorkbook.SaveAs FileName:=(FolderPath & workbook_Name), FileFormat:=6
End Sub
Okay, I was able to make a few changes to get my code to work.
Here is the final code:
Sub CSV_Aggregate()
'
'
'
'
Dim CSVAggregation As Worksheet
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
' Points the macro to the proper data source (UPDATE THIS LINE TO YOUR DATA SOURCE!!!)
FolderPath = "\\usilsvr01\lin#mktg\Analytical Services\DIA\Offers Data Question to Exclude\"
' Creates a blank workbook to host the aggregation, and names the first worksheet appropriately.
Set CSVAggregation = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Sheets(1).Name = "DIA Aggregation"
' Heads the worksheet with the relevant fields to be aggregated.
CSVAggregation.Range("A1:C1") = Array("Manufacturer Number", "Offer Code", "Data Question")
' Incrementer to keep track of where new rows should be appended.
NRow = 2
Dim LastRow As Long
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.csv")
' Loop until all .csv files in the source folder have been read.
Do While FileName <> ""
' Macro should skip over the previous version of the aggregate file
If InStr(1, FileName, "Aggregate") > 0 Then
FileName = Dir()
End If
' Open a workbook in the folder.
Set WorkBk = Workbooks.Open(FolderPath & FileName, , True)
' Loop through data sheets to collect data.
Sheets(1).Activate ' Make the sheet active, find where the data is, and select the data.
LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
After:=WorkBk.Worksheets(1).Cells.Range("A1"), _
SearchDirection:=xlPrevious, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows).Row
Set SourceRange = WorkBk.Worksheets(1).Range("A2:C" & LastRow)
' Set the destination range to start at column A and
' be the same size as the source range.
Set DestRange = CSVAggregation.Range("A" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increment NRow so that data is not overwritten.
NRow = NRow + DestRange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all data is readable.
CSVAggregation.Columns.AutoFit
CSVAggregation.Rows.AutoFit
' Places cursor on the first sell so document doesn't open highlighted or anywhere besides the top.
CSVAggregation.Range("A1").Select
' Creates variable to hold SaveAs name for Aggregation Report.
Dim workbook_Name As String
workbook_Name = "CSV Aggregate"
' Saves the workbook in the folder that the data is found in (BE SURE TO CHECK TAHT YOU HAVE THE FOLDER/FILES WITH WHICH YOU SHOULD BE WORKING!!!!)
ActiveWorkbook.SaveAs FileName:=(FolderPath & workbook_Name), FileFormat:=6
End Sub
I added a final "\" at the FilePath declaration.
I also rewrote the set WorkBk line as:
Set WorkBk = Workbooks.Open(FolderPath & FileName, , True)
This resolved the "read only" error I was getting.
You can use the pushd command to get around the UNC/network folder issue in cmd. It assigns a temporary drive letter to the network folder and allows you to continue as normal.
VBA - As the typical question starts, I am NEW (brand new) to VBA. I want to open a spreadsheet that will allow me to open multilple files (undetermined number) from one folder. It will then select certain cells from each file, copy and paste them into my original spreadsheet. Of course, then close all of the other files.
See if this will help. Because we're copying from an irregular (non-contiguous) range, it's a bit difficult to copy to another irregular range. So for that reason, the target range is "A1,B1,C1,D1,E1, etc", instead of "A1,B1,C1,E1,H1, etc". If that doesn't work for you, we'll need to try something a bit more elaborate.
Sub copyMultFiles()
Dim rS As Range, rT As Range, Cel As Range
Dim wBs As Workbook 'source workbook
Dim wS As Worksheet 'source sheet
Dim wT As Worksheet 'target sheet
Dim x As Long 'counter
Dim c As Long
Dim arrFiles() As String 'list of source files
Dim myFile As String 'source file
' change these to suit requirements
Const csMyPath As String = "C:\Documents and Settings\Dave\Desktop\TestFolder\" 'source folder
Const csMyFile As String = "*.xls" 'source search pattern
Const csSRng As String = "$C$1,$C$10,$C$11,$C$34,$D$1" 'source range
Const csTRng As String = "$A$1" 'target range
Application.ScreenUpdating = False
' target sheet
Set wT = ThisWorkbook.Worksheets(1) 'change to suit
' clear sheet
wT.Cells.Clear 'may not want this, comment out!!!
' aquire list of files
ReDim arrFiles(1 To 1)
myFile = Dir$(csMyPath & csMyFile, vbNormal)
Do While Len(myFile) > 0
arrFiles(UBound(arrFiles)) = myFile
ReDim Preserve arrFiles(1 To UBound(arrFiles) + 1)
myFile = Dir$
Loop
ReDim Preserve arrFiles(1 To UBound(arrFiles) - 1)
Set rT = wT.Range(csTRng)
' loop thru list of files
For x = 1 To UBound(arrFiles)
Set wBs = Workbooks.Open(csMyPath & arrFiles(x), False, True) 'open wbook
Set wS = wBs.Worksheets(1) 'change sheet to suit
c = 0
Set rS = wS.Range(csSRng)
'copy source range to current target row
For Each Cel In rS
Cel.Copy rT.Offset(, c) 'next column
c = c + 1
Next Cel
wBs.Close False
Set rT = rT.Offset(1) 'next row
DoEvents
Next x 'next book
Erase arrFiles
Application.ScreenUpdating = True
End Sub
I'm trying to optimize some code that takes some test data stored in CSV files does some analysis and copies their data into an excel sheet. This code is often run on hundreds of tests at a time, and its taking about 4.5 seconds per test so it can take hours to complete at times.
I looked up some optimization techniques and cut it down by about .25 seconds per test but I think the bulk of the time is being taken up by excel having to "open" the individual files before it can do anything with them. Is there a way to do this more efficiently?
I am open to answers that involve using another language to compile the files into one big file if that would make things quicker.
I would open them as text rather than workbooks:
Sub ReadCSV()
Dim MyString As String
Open "C:\path\text.csv" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, MyString ' Read a line into variable
Debug.Print MyString ' Print data to the Immediate window.
Loop
Close #1 ' Close file.
End Sub
This will be much faster than opening as a workbook
I have this function working greate handling lot of CSV files. You need to indicate in cell "D11" the name of folder containing all the CSV files and will combine them into one single file. I handle over 200 files and make it quick. Hope it helps.
Sub CombineAllFilesInADirectory()
Dim Path As String 'string variable to hold the path to look through
Dim FileName As String 'temporary filename string variable
Dim tWB As Workbook 'temporary workbook (each in directory)
Dim tWS As Worksheet 'temporary worksheet variable
Dim aWS As Worksheet 'active sheet in master workbook
Dim RowCount As Long 'Rows used on master sheet
Dim uRange As Range 'usedrange for each temporary sheet
Dim mWB_comb As Workbook 'master workbook exclusivo de esta funcion
Path = Sheets("CombineFiles").Range("D11").Value
Application.EnableEvents = False 'turn off events
Application.ScreenUpdating = False 'turn off screen updating
Set mWB_comb = Workbooks.Add(1) 'create a new one-worksheet workbook
Set aWS = mWB_comb.ActiveSheet 'set active sheet variable to only sheet in mWB
If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
Path = Path & Application.PathSeparator 'add "\"
End If
FileName = Dir(Path & "*.csv", vbNormal) 'set first file's name to filename variable
Application.StatusBar = "reading files, please wait."
Do Until FileName = "" 'loop until all files have been parsed
If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
For Each tWS In tWB.Worksheets 'loop through each sheet
Set uRange = tWS.Range("A4", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows.count - 1, _
tWS.UsedRange.Column + tWS.UsedRange.Columns.count - 1)) 'set used range
If RowCount + uRange.Rows.count > 65536 Then 'if the used range wont fit on the sheet
aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
Set aWS = mWB_comb.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
RowCount = 0 'reset RowCount variable
End If
If RowCount = 0 Then 'if working with a new sheet
aWS.Range("A1", aWS.Cells(3, uRange.Columns.count)).Value = tWS.Range("A1", _
tWS.Cells(3, uRange.Columns.count)).Value 'copy headers from tWS
RowCount = 3 'add one to rowcount
End If
aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.count, _
uRange.Columns.count).Value = uRange.Value 'move data from temp sheet to data sheet
RowCount = RowCount + uRange.Rows.count 'increase rowcount accordingly
Next 'tWS
tWB.Close False 'close temporary workbook without saving
End If
FileName = Dir() 'set next file's name to FileName variable
Loop
Application.StatusBar = "Ready"
mWB_comb.Sheets(1).Select 'select first data sheet on master workbook
Application.EnableEvents = True 're-enable events
Application.ScreenUpdating = True 'turn screen updating back on
'Clear memory of the object variables
Set tWB = Nothing
Set tWS = Nothing
Set mWB_comb = Nothing
Set aWS = Nothing
Set uRange = Nothing
End Sub