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

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
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.
 
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.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,214,422
Messages
6,119,395
Members
448,891
Latest member
tpierce

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top