没有增加预期结果的语法

时间:2019-04-30 10:06:31

标签: excel vba

我目前正在为excel编写宏,并使用“ i = i + 1”代码遇到了一些问题。我在宏中的其他地方使用了它,并且效果很好,但是在本节中,它无法正确递增。

由于这是一段很大的代码,因此我只包含了有问题的部分,但是,如果要正确调试此代码,我很乐意提供此代码。

我已经像同事一样检查了语法,我们正在努力找出问题的根源,对此将提供任何帮助。

Outcome

所有声明的变量:

D

im lastSite As Range: Set lastSite = wsReport.Range("I2:I1000")
    Dim expectedSite As Range: Set expectedSite = wsReport.Range("H2:H1000")
    Dim expectedLocation As Range: Set expectedLocation = wsReport.Range("G2:G1000")
    Dim lostTOA As Range: Set lostTOA = wsTOAOut.Range("G2:G1000")
    Dim lostMissing As Range: Set lostMissing = wsMissing.Range("G2:G1000")

    siteCode = wsHome.Range("Q6")

    Dim last
    Dim expected
    Dim lostOut
    Dim lostMiss

    departments = wsHome.Range("R6:R20")

按预期工作:

Dim iAllocation As Integer
    For Each dept In departments
        If dept = "" Then
            Exit For
        End If

    wsLBD.Range("A" & rowCounterDepartments).Value = dept
    For Each expected In expectedLocation
        If expected = dept And Not expected = "" Then
            iAllocation = iAllocation + 1
        End If
    Next
    wsLBD.Range("C" & rowCounterDepartments).Value = iAllocation
    iAllocation = 0
    rowCounterDepartments = rowCounterDepartments + 1
Next

无法正常工作:

rowCounterDepartments = 2
departmentsOut = wsHome.Range("R6:R20")
Dim iLoss As Integer
Dim iMissing As Integer
For Each lostOut In lostTOA
    If lostOut = "" Then
        Exit For
    End If
    For Each deptOut In departmentsOut
        If lostOut = deptOut And Not lostOut = "" Then
            iLoss = iLoss + 1
        End If
    Next
    wsLBD.Range("B" & rowCounterDepartments).Value = iLoss
    iLoss = 0
    rowCounterDepartments = rowCounterDepartments + 1
Next

期望的输出应该是每个部门的总数,但是每行仅输出1或0

1 个答案:

答案 0 :(得分:0)

现在,此问题已解决,我重新编写了这段代码,这对我来说是一个语法错误,其中有几段是错误的。