msquared99
Member
I have a macro that has been working for a while. For the last two months it has not been working. It pulls the data into the spreadsheet from Access and then just freezes. There are no errors, no nothing, just the little blue circle like it is doing something. But it just hangs there. Can someone help me to troubleshoot?
Code:
Sub RunParameterQuery()
'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer
'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase("C:Test\Access DB Test\M3 Database Export.accdb")
Set MyQueryDef = MyDatabase.QueryDefs("Commission Report")
'Step 3: Define the Parameters
With MyQueryDef
.Parameters("[Please enter a begin date]") = InputBox("Please enter a begin date")
.Parameters("[Please enter an end date]") = InputBox("Please enter an end date")
End With
'Step 4: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset
'Step 5: Clear previous contents
Sheets("RawData").Select
ActiveSheet.Range("A1:Q1000000").ClearContents
'Step 6: Copy the recordset to Excel
ActiveSheet.Range("A2").CopyFromRecordset MyRecordset
'Step 7: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
Next i
'Part 2 Create Workbooks for Reps
Dim WBO As Workbook 'original workbook
Dim WBN As Workbook 'new workbook
Dim WSO As Worksheet 'original worksheet
Dim WSN As Worksheet 'new worksheet
Dim r As Long
Dim finalrow As Long
Sheets("RawData").Activate
Set WBO = ActiveWorkbook
Set WSO = ActiveSheet
Application.ScreenUpdating = False
finalrow = WSO.Cells(Rows.Count, 1).End(xlUp).Row + 1
Workbooks.Open Filename:="C:Test\Commission ID's.xls"
WBO.Activate
Application.Calculation = xlCalculationAutomatic
WSO.Range("Q2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-16],'[Commission ID''s.xls]Active Commissions'!R3C1:R50C5,5,0)"
With WSO.Range("Q2")
.AutoFill Destination:=Range("Q2:Q" & finalrow)
End With
WSO.Sort.SortFields.Clear
WSO.Sort.SortFields.Add Key:=Range("Q2:Q" & finalrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With WSO.Sort
.SetRange Range("A1:Q" & finalrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("Q:Q").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A2").Select
' On Error Resume Next
' Columns("Q").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
' On Error GoTo 0
Cells.Find(What:="#N/A", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Range("A2").Select
LastReferral = Cells(2, 17)
startRow = 2
For r = 2 To finalrow
ThisReferral = WSO.Cells(r, 17)
If ThisReferral = LastReferral Then
'do nothing
Else
'we have a new referral starting
'copy all of the previous rows to a new workbook
lastRow = r - 1
RowCount = lastRow - startRow + 1
'create a new workbook
Set WBN = Workbooks.Add(template:=xlWBATWorksheet)
Set WSN = WBN.Worksheets(1)
'Setup the headings for the report
WSN.Cells(1, 1).Value = LastReferral
WSN.Range("A1:P1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.MergeCells = True
.Font.Bold = True
End With
WSN.Cells(2, 1).FormulaR1C1 = "=Today()-9"
WSN.Range("A2:P2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.MergeCells = True
.Font.Bold = True
.NumberFormat = "[$-409]mmmm-yy;@"
End With
WSN.Cells(2, 1).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
WSN.Cells(3, 1).Value = "Monthly Commission Report"
WSN.Range("A3:P3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.MergeCells = True
.Font.Bold = True
End With
WSN.Name = "M3"
'Sheets.Add before:=Sheets(Sheets.Count)
WSO.Range("A1:P1").Copy Destination:=WSN.Cells(5, 1)
'copy all of the records for this referral
WSO.Range(WSO.Cells(startRow, 1), WSO.Cells(lastRow, 16)).Copy Destination:=WSN.Cells(6, 1)
ActiveWindow.Zoom = 80
Columns("A:P").Select
Columns("A:P").EntireColumn.AutoFit
'StartRow = 6
endRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
'Cells(EndRow, 1).Value = "Total"
'Cells(EndRow, 16).FormulaR1C1 = "=Sum(R[" & StartRow - EndRow & "]C:R[-1]C)"
'This is new code*********************
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.PaperSize = xlPaperLetter
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
'NEW CODE to sort sheets. 6/29/15
WSN.Range("A5:P" & endRow).Select
WSN.Sort.SortFields.Clear
Selection.Sort Key1:=Range("D5"), order1:=xlAscending, Key2:=Range("B5"), order2:=xlAscending, Header:=xlGuess, Orientation:=xlSortColumns
WSN.Range("A5").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(16), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
WSN.Columns("P:P").Select
Selection.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
WSN.Range("A2").Select
With Selection
.NumberFormat = "[$-409]mmmm-yy;@"
End With
WSN.Range("B6:B20000").Select
Selection.NumberFormat = "0"
WSN.Range("E6:E20000").Select
Selection.NumberFormat = "mm/dd/yy;@"
WSN.Range("H6:H20000").Select
Selection.NumberFormat = "mm/dd/yy;@"
WSN.Range("I6:I20000").Select
Selection.NumberFormat = "mm/dd/yy;@"
Sheets.Add
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Commission Summary"
With Sheets
Range("B1").Value = ("Total")
Range("B1").Select
Selection.HorizontalAlignment = xlCenter
Selection.Font.Bold = True
Range("A2").Value = ("M3")
Range("A3").Value = ("IC")
Range("A4").Value = ("B&I")
Range("A5").Value = ("Time")
Range("A6").Value = ("CFS")
Range("A7").Value = ("Skylight")
Range("A8").Value = ("Adjustments")
Range("A9").Value = ("Bonus")
Range("A10").Value = ("Total")
Range("B10").Formula = "=SUM(B2:B9)"
Range("B10").Select
Selection.Style = "Currency"
Selection.Font.Bold = True
Columns("A:A").Select
Selection.Font.Bold = True
Columns("A:A").AutoFit
Range("A1").Select
End With
FN = LastReferral & " " & Format(Date - 9, "mm-yy") & ".xlsx"
FP = WBO.Path & Application.PathSeparator
WBN.SaveAs Filename:=FP & FN
WBN.Close SaveChanges:=Fales
LastReferral = ThisReferral
startRow = r
End If
Next r
Call IC_Commissions
Application.ScreenUpdating = True
MsgBox ("Process is complete!")
End Sub