["VBA"] countif, sort macro require a loop

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I am in a real trouble here, I understand basic loop ideas, but to loop the macro I am working now, is a hard concept.
I am using countif and the sorting function, but my array is to big, so let me show you the macros I am repeating now,
and do this more than 2000 times now I understand how important is a loop.
ok, this is what I did so far.
VBA Code:
Sub KLM()
Range("L2:L54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$2:$G$2,L2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 65535
End With
Selection.FormatConditions(1).StopIfTrue = False

'//////////////////////////////////////////////SORT BY COUNT////////////////////////////////////////////////////////////////////////////////////////

    Range("L1:M54").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("M2:M54") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("L1:M54")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub NOP()
Range("O2:O54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$3:$G$3,O2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 65535
End With
Selection.FormatConditions(1).StopIfTrue = False

'//////////////////////////////////////////////SORT BY COUNT////////////////////////////////////////////////////////////////////////////////////////

    Range("O1:P54").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("P2:P54") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("O1:P54")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Sub QRS()
Range("R2:R54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$4:$G$4,R2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 65535
End With
Selection.FormatConditions(1).StopIfTrue = False

'//////////////////////////////////////////////SORT BY COUNT////////////////////////////////////////////////////////////////////////////////////////

    Range("R1:S54").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("S2:S54") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("R1:S54")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Please give me a hand here. thank you.
I think the macro speak by itself.
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
  1. 2010
Platform
  1. Windows
Thanks for your kindness Mr. Dante.
Sorry about this, I have to run a frequency code before your code reason why I use the word "bin", sorry. the point here is that I use your code in different workbook as well, and the data just when from 54 the original code to 37, so I just change this numbers, and the column in the first one are from B:G, and in the other workbook is just B:F that's why I say 5
so here it is the message and the line to debug.
1597984655588.png

the debug line is
1597984680110.png

and again thank you for the extra attention to this tricky project.
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
  1. 2010
Platform
  1. Windows
I just open this account, thank you. Yes is true i tried and work, thanks.
 

Watch MrExcel Video

Forum statistics

Threads
1,111,703
Messages
5,541,308
Members
410,545
Latest member
Upsindustrial20
Top