Excel Vba error in 'For" statement

messi1335

I'm taking a random sample of 3% of report hits per report per market..yeah, don't ask. Anyway - I have the code all set and done, I have markets and reports set up as an array

ex: market = Array("market1", "market2","market3"...) report = Array("report1", "report2", "report3"...)

my problem is that when I run the code to get 3%, when there is no report hits for that market - I get a duplicate of the previous report.

the data looks like this:

market1 | report1 | #of hits

market1 | report2 | #of hits

Market1 | report3 | #of hits

market2 | report1 |#of hits

market2 | report1 | #of hits

market2 | report3 | #of hits

How do I eliminate the duplication of the previous data?

here is my code:

For i = 0 To UBound(market)
For z = 0 To UBound(report)

    
    reporthits = Application.CountIfs(ws2.Range("B:B"), market(i), ws2.Range("L:L"), report(z))
       
    If reporthits * 0.03 < 1 Then
    samplecount = 1
    Else
    samplecount = Round(reporthits * 0.03, 0)
    End If

    rownumber = ws2.Evaluate("IFERROR(Match(""" & market(i) & report(z) & """,B:B & L:L, 0),""Not Found"")")
        
    
    If IsNumeric(rownumber) Then

ws2.Range("$A" & rownumber & ":$L" & (rownumber + (samplecount - 1))).Copy
lrow3 = ws3.Range("A" & Rows.Count).End(xlUp).Row + 1
ws3.Range("A" & lrow3).PasteSpecial xlPasteAll

Else: Resume Next

thanks!

Scott Craner

Replace:

on error resume next
ws2.Range("$A" & rownumber & ":$L" & (rownumber + (samplecount - 1))).Copy

lrow3 = ws3.Range("A" & Rows.Count).End(xlUp).Row + 1
ws3.Range("A" & lrow3).PasteSpecial xlPasteAll

with

If Isnumeric(rownumber) Then
    ws2.Range("$A" & rownumber & ":$L" & (rownumber + (samplecount - 1))).Copy

    lrow3 = ws3.Range("A" & Rows.Count).End(xlUp).Row + 1
    ws3.Range("A" & lrow3).PasteSpecial xlPasteAll
End If

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Related Related

HotTag

Archive