mellymelle
New Member
- Joined
- Nov 25, 2016
- Messages
- 45
In a nutshell, my code loops through a master database and compiles a report for each unique autofilter criteria of a field. There are fees and other calculations for each criteria, and as there are hundreds of criteria I'm using the same control variable for each unique criteria's calcs. As such I have to reset these variables to zero at the beginning of each loop but it seems to reset to zero before the output steps. Weird thing the fees calc output is working but not the others (these print on report as zero), even though coding sequences are same for all. Would anyone be so kind as to take a look and see if you can spot what's going on? I do have a debug.print of one of the calcs that's not working and they print as non-zeros in the immediate window. Thanks in advance. If you notice any other wrongs/bad habits etc., any and all advice is welcome to a rookie like me.
Code:
For currentCell = 2 To NumFilters
Sheet9.UsedRange.Clear
sumFees = 0
sumHST = 0
sumGST = 0
sumQST = 0
sumPST = 0
reportLine = 9
currentAcct = Sheet8.Range("A" & currentCell).Value
currentClient = Sheet8.Range("B" & currentCell).Value
Sheet2.UsedRange.AutoFilter Field:=2, Criteria1:=currentAcct
Sheet2.UsedRange.SpecialCells(xlCellTypeVisible).Copy Sheet9.Range("A1")
numTrans = Sheet9.Cells(Rows.Count, "A").End(xlUp).Row
Sheet9.Sort.SortFields.Clear
Sheet9.Sort.SortFields.Add Key:=Range("D2:D" & numTrans), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Sheet9.Sort
.SetRange Range("A1:H" & numTrans)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheet1.Range("B5").Formula = currentAcct
Sheet1.Range("B6").Formula = currentClient
For rowNumber = 1 To numTrans
If reportLine = 35 Then
Sheet1.Range("A42").Clear
Sheet1.Range("A5:H8").Copy Sheet1.Range("A48")
Sheet1.Range("A36:B46").Copy Sheet1.Range("A83")
Sheet1.Range("I37").Value = sumFees
Sheet1.Range("I38").Value = sumHST
Sheet1.Range("I39").Value = sumGST
Sheet1.Range("I40").Value = sumQST
Sheet1.Range("I41").Value = sumPST
reportLine = 53
End If
Trans = Left(Sheet9.Range("F" & rowNumber).Value, 3)
Select Case Trans
Case "MGF"
Sheet9.Range("D" & rowNumber).Copy Sheet1.Range("A" & reportLine)
Sheet9.Range("G" & rowNumber).Copy Sheet1.Range("C" & reportLine)
Sheet9.Range("E" & rowNumber).Copy Sheet1.Range("I" & reportLine)
sumFees = sumFees + Sheet9.Range("E" & rowNumber).Value
reportLine = reportLine + 1
Case "HSM"
sumHSM = sumHSM + Sheet9.Range("E" & rowNumber).Value
Debug.Print sumHSM
Case "GSM"
sumGSM = sumGSM + Sheet9.Range("E" & rowNumber).Value
Case "QSM"
sumQSM = sumQSM + Sheet9.Range("E" & rowNumber).Value
Case "PSM"
sumPSM = sumPSM + Sheet9.Range("E" & rowNumber).Value
End Select
Next rowNumber
If Not reportLine > 35 Then
Sheet1.Range("I37").Value = sumFees
Sheet1.Range("I38").Value = sumHST
Sheet1.Range("I39").Value = sumGST
Sheet1.Range("I40").Value = sumQST
Sheet1.Range("I41").Value = sumPST
Sheet1.Range("I42").Formula = "=SUM(R[-5]C:R[-1]C)"
Sheet1.Range("I42").Font.Bold = True
Else
Sheet1.Range("I84").Value = sumFees
Sheet1.Range("I85").Value = sumHST
Sheet1.Range("I86").Value = sumGST
Sheet1.Range("I87").Value = sumQST
Sheet1.Range("I88").Value = sumPST
Sheet1.Range("I89").Formula = "=SUM(R[-5]C:R[-1]C)"
Sheet1.Range("I89").Font.Bold = True
End If
Sheet1.Columns(9).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* " - "??_);_(@_)"
Sheet1.UsedRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
CreateObject("WScript.shell").specialfolders("Desktop") & "\HRCreports\" & currentClient & " " & currentAcct & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Sheet1.Range("A9:I35").ClearContents
Sheet1.Range("I37:I42").ClearContents
Sheet1.Range("A47:I93").ClearContents
Next currentCell