0

Every month i get a billing record of roughly 5,000 lines in whats called a flat file. within the 5,000+ lines there are multiple billing types (read billable services). So example. Customer A could have 18 different bill types and customer B could have 25.
Each bill type has 2 lines, a header line and a data line. and each bill type has different headers and the number of columns. Except for the 1st 3 which is always customer number, date, record type(bill type). in addition each bill type will need to have their own worksheet.

So here's what I've done.

Sub BillType2()

'Clearing the destination worksheet of previous data

Sheets("REC_type_2_summary").Activate
Rows("2:2").Select
Selection.AutoFilter
Range("B3:I3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Clear

'back to source file

Sheets("CGT_REPORT (3)").Activate
Rows("1:1").Select
Application.CutCopyMode = False
Selection.AutoFilter

'setting filter for record type and eliminating the header rows.

ActiveSheet.Range("$A$1:$AL$14637").AutoFilter Field:=3, Criteria1:="2"
ActiveSheet.Range("$A$1:$AL$14637").AutoFilter Field:=4, Criteria1:="<>*Exhibit*"

Range("A2:H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

'Pasting result back to the destination sheet for that record type

 Sheets("REC_type_2_summary").Activate
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Columns(3).NumberFormat = "mm/dd/yy"
Range("C1").NumberFormat = "###"

If Range("C1") > 0 Then
Rows("2:2").Select
Selection.AutoFilter Field:=10, Criteria1:="<>0"
End If

End Sub

Now I do a similar process with about 35 other bill types, each with just slightly different criterias. then i put them all together in one large macro called runAll which is just basically me calling all 35 macros.

70% of the time it runs fine. however other times it either takes for ever or i'll get the error that shows I'm running out of resources. How to make this more efficient?

Cœur
  • 37,241
  • 25
  • 195
  • 267
James Chen
  • 237
  • 2
  • 6
  • 18
  • 2
    get rid of all the `.Select` and `.Activate` see: https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – Scott Craner Feb 23 '18 at 17:53
  • Also you can turn of Screen Updating and set Calculation to Manual at the top, then reset at the end. – BruceWayne Feb 23 '18 at 18:01

1 Answers1

3

I took a shot at cleaning this up for you, getting rid of Select and Activate. One more thing that I didn't look into was checking rows to make sure if they're already auto-filtered or not - that could be an issue, if you're not certain of their status.

Note that instead of referencing the worksheets over and over, we just assign them to sht1 and sht2 at the start.

This should run A LOT faster (if set up properly, I did NOT test on my computer).

Sub BillType2()

'Run faster
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'Get some lastrow values set up here
Dim sht1 As Worksheet, sht2 As Worksheet, lastrow As Long
Set sht1 = ThisWorkbook.Worksheets("REC_type_2_summary")
Set sht2 = ThisWorkbook.Worksheets("CGT_report (3)")
lastrow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row

'Clearing the destination worksheet of previous data
sht1.Rows("2:2").AutoFilter
sht1.Range("B3:I" & lastrow).ClearContents

'back to source file

lastrow = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row

sht2.Rows("1:1").AutoFilter

'setting filter for record type and eliminating the header rows.

sht2.Range("$A$1:$AL$" & lastrow).AutoFilter Field:=3, Criteria1:="2"
sht2.Range("$A$1:$AL$" & lastrow).AutoFilter Field:=4, Criteria1:="<>*Exhibit*"

lastrow = sht2.Cells(sht.Rows.Count, "A").End(xlUp).Row

sht1.Range("B3:I" & lastrow + 1).Value = _
sht2.Range("A2:H" & lastrow).Value

sht1.Columns(3).NumberFormat = "mm/dd/yy"
sht1.Range("C1").NumberFormat = "###"

If sht1.Range("C1") > 0 Then
    sht1.Rows("2:2").AutoFilter Field:=10, Criteria1:="<>0"
End If

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
dwirony
  • 5,487
  • 3
  • 21
  • 43
  • i fixed some typos and i hope i did it right. but i'm still having a problem. instead of picking up only record type 2 without the header "exhibit" on column 4 it's picking up all record types and the header. Other than that this is amazing. code's too long to post i just correct some of the sht – James Chen Feb 23 '18 at 18:44
  • @JamesChen If the answer helped, please, mark it as correct. – Foxfire And Burns And Burns Feb 23 '18 at 21:07
  • @JamesChen I'm not sure what part of your code is moving that data - are you sure the data was filtered probably? – dwirony Feb 23 '18 at 21:20
  • i figured it out. sorry for the late response. my daughter had the flu and this is my first day back to work. – James Chen Feb 27 '18 at 17:11
  • @dwirony when stepping through the function it filters properly, however when taking it to the destination page it pastes everything. filtered or not. in the interest of time i just replaced it with my old pastevalues code. not the most efficient but works. i figured hey, dwirony just replaced my hyundai with a mercedes, whats an old air filter gonna hurt. – James Chen Feb 27 '18 at 17:15