["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.
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
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
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
  1. 2010
Platform
  1. Windows
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,
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
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.
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

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.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
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)"
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Thank you one more time you are excellent, work perfect.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
  1. 2010
Platform
  1. Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
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?
 

Watch MrExcel Video

Forum statistics

Threads
1,112,865
Messages
5,542,960
Members
410,579
Latest member
bdubz
Top