I was wondering if anyone out there had any suggestions on how to make this code more simple and not so long and repetitive. What I have is 2 combo boxes, one with a detailer's name and the other with a month. Depending on what is picked, it filters another worksheet and fills in values from that worksheet. I guess what I am looking for is to not repeat this code 4 times.
Any help would be appreciated.
Code:
With Worksheets("filtered")
.Range("B5") = rng.Cells(1, "FQ")
.Range("B6") = rng.Cells(1, "FV")
.Range("I5") = rng.Cells(1, "GA")
.Range("I11") = rng.Cells(1, "FZ")
.Range("I12") = rng.Cells(1, "FY")
.Range("I13") = rng.Cells(1, "FX")
.Range("I14") = rng.Cells(1, "FW")
.Range("B11") = rng.Cells(1, "FP")
.Range("B12") = rng.Cells(1, "FO")
.Range("B13") = rng.Cells(1, "FN")
.Range("B14") = rng.Cells(1, "FM")
For x = 1 To 46
.Range("B40").Offset(x - 1) = rng.Cells(1, "BK").Offset(, x - 1)
Next x
For y = 1 To 20
.Range("J40").Offset(y - 1) = rng.Cells(1, "DE").Offset(, y - 1)
Next y
End With
Code:
Private Sub cmdFilterDetailer_Click()
Dim rng As Range
Dim x As Long
Dim y As Long
Set rng = Worksheets("part number").Range("Subtotals")
Worksheets("part number").AutoFilterMode = False
With Worksheets("part number").Columns("G:L")
.AutoFilter
If cboDetailFilter = "All" And cboDateFilter = "All" Then
On Error Resume Next
With Worksheets("filtered")
.Range("B5") = rng.Cells(1, "FQ")
.Range("B6") = rng.Cells(1, "FV")
.Range("I5") = rng.Cells(1, "GA")
.Range("I11") = rng.Cells(1, "FZ")
.Range("I12") = rng.Cells(1, "FY")
.Range("I13") = rng.Cells(1, "FX")
.Range("I14") = rng.Cells(1, "FW")
.Range("B11") = rng.Cells(1, "FP")
.Range("B12") = rng.Cells(1, "FO")
.Range("B13") = rng.Cells(1, "FN")
.Range("B14") = rng.Cells(1, "FM")
For x = 1 To 46
.Range("B40").Offset(x - 1) = rng.Cells(1, "BK").Offset(, x - 1)
Next x
For y = 1 To 20
.Range("J40").Offset(y - 1) = rng.Cells(1, "DE").Offset(, y - 1)
Next y
End With
ElseIf cboDetailFilter.Value = "All" And cboDateFilter <> "All" Then
.AutoFilter Field:=6, Criteria1:=cboDateFilter.Value
On Error Resume Next
With Worksheets("filtered")
.Range("B5") = rng.Cells(1, "FQ")
.Range("B6") = rng.Cells(1, "FV")
.Range("I5") = rng.Cells(1, "GA")
.Range("I11") = rng.Cells(1, "FZ")
.Range("I12") = rng.Cells(1, "FY")
.Range("I13") = rng.Cells(1, "FX")
.Range("I14") = rng.Cells(1, "FW")
.Range("B11") = rng.Cells(1, "FP")
.Range("B12") = rng.Cells(1, "FO")
.Range("B13") = rng.Cells(1, "FN")
.Range("B14") = rng.Cells(1, "FM")
For x = 1 To 46
.Range("B40").Offset(x - 1) = rng.Cells(1, "BK").Offset(, x - 1)
Next x
For y = 1 To 20
.Range("J40").Offset(y - 1) = rng.Cells(1, "DE").Offset(, y - 1)
Next y
End With
ElseIf cboDateFilter = "All" And cboDetailFilter.Value <> "All" Then
.AutoFilter Field:=1, Criteria1:=cboDetailFilter.Value
On Error Resume Next
With Worksheets("filtered")
.Range("B5") = rng.Cells(1, "FQ")
.Range("B6") = rng.Cells(1, "FV")
.Range("I5") = rng.Cells(1, "GA")
.Range("I11") = rng.Cells(1, "FZ")
.Range("I12") = rng.Cells(1, "FY")
.Range("I13") = rng.Cells(1, "FX")
.Range("I14") = rng.Cells(1, "FW")
.Range("B11") = rng.Cells(1, "FP")
.Range("B12") = rng.Cells(1, "FO")
.Range("B13") = rng.Cells(1, "FN")
.Range("B14") = rng.Cells(1, "FM")
For x = 1 To 46
.Range("B40").Offset(x - 1) = rng.Cells(1, "BK").Offset(, x - 1)
Next x
For y = 1 To 20
.Range("J40").Offset(y - 1) = rng.Cells(1, "DE").Offset(, y - 1)
Next y
End With
Else
.AutoFilter Field:=6, Criteria1:=cboDateFilter.Value
.AutoFilter Field:=1, Criteria1:=cboDetailFilter.Value
On Error Resume Next
With Worksheets("filtered")
.Range("B5") = rng.Cells(1, "FQ")
.Range("B6") = rng.Cells(1, "FV")
.Range("I5") = rng.Cells(1, "GA")
.Range("I11") = rng.Cells(1, "FZ")
.Range("I12") = rng.Cells(1, "FY")
.Range("I13") = rng.Cells(1, "FX")
.Range("I14") = rng.Cells(1, "FW")
.Range("B11") = rng.Cells(1, "FP")
.Range("B12") = rng.Cells(1, "FO")
.Range("B13") = rng.Cells(1, "FN")
.Range("B14") = rng.Cells(1, "FM")
For x = 1 To 46
.Range("B40").Offset(x - 1) = rng.Cells(1, "BK").Offset(, x - 1)
Next x
For y = 1 To 20
.Range("J40").Offset(y - 1) = rng.Cells(1, "DE").Offset(, y - 1)
Next y
End With
End If
.AutoFilter
End With
ActiveSheet.Rows("3:3").Hidden = True
End Sub