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

montecarlo2012

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

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I don't think it's good practice to have 2000 conditional formatting, but check out the following:

The macro goes from row 2 to the last row with data according to column B. If you want it to go from 2 to 2000, change this line:
For i = 2 To Range("B" & Rows.Count).End(3).Row
For this:
For i = 2 To 2000

Try this:
VBA Code:
Sub Loop_FC()
  Dim i As Long, j As Long
  Dim c As String
 
  Application.ScreenUpdating = False
 
  j = Columns("L").Column
  Cells.FormatConditions.Delete

  For i = 2 To Range("B" & Rows.Count).End(3).Row
    c = Split(Cells(1, j).Address, "$")(1)
    Range(Cells(2, j), Cells(54, j)).Select
    With Selection.FormatConditions
      .Add Type:=xlExpression, Formula1:="=COUNTIF($B$" & i & ":$G$" & i & "," & c & "2)"
      With Selection.FormatConditions(.Count)
        .Interior.Color = 65535
        .StopIfTrue = False
        .SetFirstPriority
      End With
    End With
    'SORT BY COUNT
    Range(Cells(1, j), Cells(54, j + 1)).Sort Cells(2, j + 1), xlDescending, Header:=xlYes
    j = j + 3
  Next
 
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi.
Mr. DanteAmor GREAT JOB,
I have a little question if you don't mind.
the highlight countif how can I change that, looks like simple but, if don't change this then all the results becoming useless, sorry. let me show you Please, the difference



VBA Code:
Sub KLM()
Range("L2:L54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$1:$G$1,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$2:$G$2,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


My mistake was to run the frequency on B2:G4 and highlight B2:G2, this is wrong, the change in this macro now is run the frequency from B3:G5 and highlight B2:G2, so after will be B4:G6 highlight B3:G3 and so on. The idea of highlight has to be out of the frequency zone, then the information will be useful.
Sorry I didn't see this before, can you Please do that, Thank you,
 
Upvote 0
Post #1

Rich (BB code):
Range("L2:L54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$2:$G$2,L2)"

Range("O2:O54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$3:$G$3,O2)"

Range("R2:R54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$4:$G$4,R2)"

Post #3
Rich (BB code):
Range("L2:L54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$1:$G$1,L2)"

Range("O2:O54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$2:$G$2,O2)"

Range("R2:R54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF(?????)"


I'm confused.
What is the correct code?
I do not understand what the problem is.
 
Upvote 0
Sorry, about that. the correct one is Number 3.
VBA Code:
Range("L2:L54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$1:$G$1,L2)"

Range("O2:O54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$2:$G$2,O2)"

Range("R2:R54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$3:$G$3,R2)"

Range("U2:U54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$4:$G$4,U2)"

Range("X2:X54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$5:$G$5,X2)"

Range("AA2:AA54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$6:$G$6,AA2)"

Range("AD2:AD54").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($B$7:$G$7,AD2)"

Really sorry and thank you for taking care my case.
 
Upvote 0
Change this:
VBA Code:
.Add Type:=xlExpression, Formula1:="=COUNTIF($B$" & i & ":$G$" & i & "," & c & "2)"

For this:
VBA Code:
.Add Type:=xlExpression, Formula1:="=COUNTIF($B$" & i - 1 & ":$G$" & i - 1 & "," & c & "2)"
 
Upvote 0
Mr. DanteAmor sorry to bother you. in case is possible before you left.
I tried with a array of 5 columns and give me and error on
c = Split(Cells(1, j).Address, "$")(1)
I just chance the bin number
VBA Code:
Sub FQ_NIG()
'danteamor

  Dim i As Long, j As Long
  Dim c As String
 'I don't think it's good practice to have 2000 conditional formatting
  Application.ScreenUpdating = False
 
  j = Columns("L").Column
  Cells.FormatConditions.Delete

  For i = 2 To Range("B" & Rows.Count).End(3).Row
  'for i = 2 to 2000
    c = Split(Cells(1, j).Address, "$")(1)
    Range(Cells(2, j), Cells(37, j)).Select
    With Selection.FormatConditions
    .Add Type:=xlExpression, Formula1:="=COUNTIF($B$" & i - 1 & ":$G$" & i - 1 & "," & c & "2)"
      '.Add Type:=xlExpression, Formula1:="=COUNTIF($B$" & i & ":$G$" & i & "," & c & "2)"
      With Selection.FormatConditions(.Count)
        .Interior.Color = 65535
        .StopIfTrue = False
        .SetFirstPriority
      End With
    End With
    'SORT BY COUNT
    Range(Cells(1, j), Cells(37, j + 1)).sort Cells(2, j + 1), xlDescending, Header:=xlYes
    j = j + 3
  Next
 
  Application.ScreenUpdating = True
End Sub
just in case is possible a little look here. thanks
 
Upvote 0
What does the error message say?
how many rows with data do you have in column B?
What is "bin number"?
what do you mean by array of 5 columns?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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