Help: Loop without Do, why?

NRGZ

New Member
Joined
Jul 23, 2021
Messages
47
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. Mobile
  3. Web
I dont know why this errors but i assume its because it has a 'WITH' within the loop. I'm not sure how i can get around it so if anyone can help it would be appreciated.

VBA Code:
' counts how many unique entries there are in a range

Sub dupez3()

Dim ws As Worksheet
Dim ws1 As Worksheet
Dim shts As Integer
Dim a As Integer

shts = ThisWorkbook.Sheets.Count

a = 3

Do Until a = shts + 1

' Set ws = Worksheets("sheet" & a)
ws = Worksheets("sheet" & a)
' Set ws1 = Worksheets("Totals")
ws1 = Worksheets("Totals")
' Sheets("sheet2").Activate

With ws.Range("D2", ws.Range("D" & Rows.Count).End(xlUp))
   
   ws1.Cells(3, 3) = ws.Evaluate("SUMPRODUCT(1/COUNTIF(" & .Address & "," & .Address & "))")
   
   ' MsgBox Evaluate("SUMPRODUCT(1/COUNTIF(" & .Address & "," & .Address & "))")

a = a + 1

Loop

End With

End Sub

Many thanks,
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
These two should be the other way round
VBA Code:
Loop

End With
 
Upvote 0
Solution
VBA Code:
Sub dupez3()

Dim ws As Worksheet
Dim ws1 As Worksheet
Dim shts As Integer
Dim a As Integer

shts = ThisWorkbook.Sheets.Count
a = 3
Set ws1 = Worksheets("Totals")

Do Until a > shts
    Set ws = Worksheets("sheet" & a)
    With ws.Range("D2", ws.Range("D" & Rows.Count).End(xlUp)) 
        ws1.Cells(3, 3) = ws.Evaluate("SUMPRODUCT(1/COUNTIF(" & .Address & "," & .Address & "))") 
        ' MsgBox Evaluate("SUMPRODUCT(1/COUNTIF(" & .Address & "," & .Address & "))")
        a = a + 1
    End With
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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