简体   繁体   中英

Excel Vba error in 'For" statement

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM