Excel中的Excel Vba错误' For"声明

时间:2017-04-21 19:46:13

标签: excel excel-vba vba

我随机抽取每个市场每份报告中3%的报告点击率。不,不要问。无论如何 - 我已完成所有设置和完成的代码,我将市场和报告设置为数组

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

我的问题是,当我运行代码获得3%时,当该市场没有报告点击时 - 我得到了上一份报告的副本。

数据如下所示:

market1 | report1 | #of hits

market1 | report2 | #of hits

Market1 | report3 | #of hits

market2 | report1 | #of hits

market2 | report1 | #of hits

market2 | report3 | #of hits

如何消除以前数据的重复?

这是我的代码:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

替换:

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

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