Hi all,
I am working on something to help me analyze a bunch of queries in an Access database. To motivate this I exported all the queries to .csv in a particular folder. I want the output to be in a format so that I can filter on the query and pull up the associated SQL statements for quick review within Excel.
In "A1" of each file exists the name of the query. From "A2" until the end of values (only 2 to 5 rows for any given file) is a SQL statement. First I want to copy the SQL statements to column B, then I want to copy the query name on to every row in A where there is a SQL statement in B. Last, I want to copy the complete, manipulated data to a Summary worksheet. Since my code loops through every file in the folder, I need to dynamically update the rows to paste to in the Summary sheet.
I was able to get the code to work on a small number of files (2-5), but it failed when I attempted to run on all the files in the folder. I believe the operations are overloading the memory of the application, and that my setting of ranges is interfering with the application.
I appreciate any feedback. I've left all the comments so that you can see what I tried previously.
I am working on something to help me analyze a bunch of queries in an Access database. To motivate this I exported all the queries to .csv in a particular folder. I want the output to be in a format so that I can filter on the query and pull up the associated SQL statements for quick review within Excel.
In "A1" of each file exists the name of the query. From "A2" until the end of values (only 2 to 5 rows for any given file) is a SQL statement. First I want to copy the SQL statements to column B, then I want to copy the query name on to every row in A where there is a SQL statement in B. Last, I want to copy the complete, manipulated data to a Summary worksheet. Since my code loops through every file in the folder, I need to dynamically update the rows to paste to in the Summary sheet.
I was able to get the code to work on a small number of files (2-5), but it failed when I attempted to run on all the files in the folder. I believe the operations are overloading the memory of the application, and that my setting of ranges is interfering with the application.
I appreciate any feedback. I've left all the comments so that you can see what I tried previously.
Code:
Sub MergeQueries_partOne()
' Macro manipulates data, leaving the top cell in column A, but moves everything from A2 down over to column B.
' The macro then copies the data in A1 down the empty cells in A where there is a value in column B.
' The final piece of the macro moves the finished, manipulated data to a summary workbook and worksheet, pasting below the last
' used row of previous copies. The macro will produce a summary workbook of all the dat in all files in the folder.
' partOne simply moves the SQL query statements to column B
' Declare variables
Dim SummaryWB As Workbook
Dim SummarySheet As Worksheet
Dim queryPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange1 As Range
Dim SourceRange2 As Range
Dim DestRange1 As Range
Dim DestRange2 As Range
Dim r1Count As Long
Dim r2Count As Long
Dim r3Count As Long
Dim queryName As String
Dim queryCopy As Range
Dim copyRange As Range
Dim pasteRange As Range
' Set app updating properties
With Application
.Calculation = CalcMode
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Create a new workbook and set a variable to the first sheet
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
' Set SummarySheet = ThisWorkbook.Worksheets(1)
' Considered using ThisWorkbook property to make the summary workbook equal to the open workbook where macro was created
' Folder path - using dummy path
queryPath = "C:\Users\ALEX\Desktop\QueryCsvs\"
' Initialize variable to track last used row
NRow = 1
' Call Dir for first time, pointing to all .csv files in folder path
FileName = Dir(queryPath & "*.csv")
' Loop until Dir returns an empty string
Do While FileName <> ""
Set WorkBk = Workbooks.Open(queryPath & FileName) 'Set working file from folder
'Activate WorkBk
WorkBk.Activate
'Count number of initial rows
'r1Count = WorkBk.Worksheets(1).Cells(.Rows.Count, "A").End(xlUp).Row - first attempt
r1Count = ActiveSheet.Range("A1", ActiveSheet.Cells(Rows.Count, 1).End(xlUp)).Rows.Count
r2Count = r1Count - 1 ' Calculate for paste into column B
'Set the source range of initial data
Set SourceRange1 = WorkBk.Worksheets(1).Range("A2:A" & r1Count)
Set SourceRange2 = WorkBk.Worksheets(1).Range("B1:B" & r2Count)
'Copy SQL code over to column B in source workbook
SourceRange1.Select
SourceRange1.Copy Destination:=SourceRange2
'Now clear values out of SourceRange1
SourceRange1.ClearContents
'Now use msgBox to print results during testing
'MsgBox ("r1Count is equal to: " & r1Count & ". r2Count is equal to: " & r2Count)
'Copy the query name, that is the string value of A1
WorkBk.Worksheets(1).Range("A1").Copy Destination:=WorkBk.Worksheets(1).Range("A2:A" & r2Count)
'Declare a variable to help define the bottom and the rightmost cell of manipulated data to aid in transfer
'to summary sheet
r3Count = NRow + r2Count
SummarySheet.Activate
'Next two comments were original attempt to set the range to paste to in the summary book
'Set pasteRange = SummarySheet.Range(Cells("A" & NRow & ":B" & r3Count)
'Set pasteRange = pasteRange.Resize(r2Count, 2)
'This is the code that succeeded in pasting data to summary sheet
WorkBk.Worksheets(1).Range("A1:B" & r2Count).Copy Destination:=SummarySheet.Cells(NRow, 1)
'Below code did not run, but attempted to handle dynamic ranges
'Now select copy range in source workbook
'Set queryCopy = WorkBk.Worksheets(1).Range("A1:B" & r2Count)
'Set target range for summary workbook and ensure same size as source range
'error handling for first case
'If NRow = 1 Then
' SummarySheet.Activate
' Set pasteRange = SummarySheet.Range("A" & NRow)
' Set pasteRange = pasteRange.Resize(r2Count, 2)
'Else
' Set pasteRange = SummarySheet.Range("A" & NRow & ":B" & r3Count)
' Set pasteRange = pasteRange.Resize(r2Count, 2)
'End If
'Copy and paste from source to destination
'queryCopy.Copy Destination:=pasteRange
'queryCopy.Value = pasteRange.Value
' increase NRow each iteration
'NRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row + 1 ' Choose next empty row after lastrow used
NRow = NRow + r2Count
'or try:
'NRow = Nrow + pasteRange.Rows.Count
'Now close source workbook without saving changes
WorkBk.Close SaveChanges:=False
' Use Dir to get next file name
FileName = Dir()
Loop
'Call autofit for readability of info
SummarySheet.Select
SummarySheet.Columns.AutoFit
SummarySheet.SaveAs queryPath & "TEST\" & "QueryTestResultsA"
' release application properties
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub