This code is SOOOOO close to being right, but I cannot for the life of me figure out what I am missing. The part where I autofill the formula to the last row only works on the first sheet and not the rest. I'm sure its something dumb that I'm missing because I am new to the last row function. Any suggestions?
Rich (BB code):
Sub AM4_2()
'
' AM4_2 Macro
'
' Adds two new columns with if statements for less than 10%. Sorts by Yes-Less than 10% then by Bill Count and Allowed
'
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
With ws
.Columns("BC:BC").Insert Shift:=xlToRight
.Range("BC1").Value = ">10%"
With .Range("BC2")
.FormulaR1C1 = "=IF(RC[-1]>10%,""yes"",""no"")"
End With
With .Range("BC2")
Lastrow& = Range("BA:BB").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range("BC2").AutoFill Destination:=Range("BC2:BC" & Lastrow)
.Value = .Value
End With
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range("BC2"), SortOn:=xlSortOnValues, _
Order:=xlAscending, CustomOrder:="no,yes", DataOption:=xlSortNormal
With .Sort
.SetRange Parent.Range("AO:BC")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
.Sort.SortFields.Add Key:=.Range("AQ2"), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
With .Sort
.SetRange Parent.Range("AO:BC")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With .Range("BS2")
.FormulaR1C1 = "=IF(RC[-1]>10%,""yes"",""no"")"
Lastrow& = Range("BP:BQ").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range("BS2").AutoFill Destination:=Range("BS2:BS" & Lastrow)
.Value = .Value
End With
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range("BS2" _
), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="no,yes", _
DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=.Range("BH2" _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With .Sort
.SetRange Parent.Range("BE:BS")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Next
Application.ScreenUpdating = True
End Sub