【问题标题】:Variable is not taking in value?变量没有取值?
【发布时间】:2019-09-20 13:58:30
【问题描述】:

使用 Excel VBA,我声明了两个变量(RowNumberPlus1RowNumber)并创建了一个 do while loop 以及嵌套在其中的 if, else 语句。
我正在尝试使用 RowNumberPlus1 = RowNumber + 1 进行迭代,因为我想将 RowNumber 的值保留在特定的 ElseIf 语句之外。
但是,我的 RowNumberPlus1 没有接受该表达式并保持为 0,即我启动它的值。

代码如下所示:

Sub FormatBreakerSettingsData()

'Define variables for the Breaker Settings Fields
Dim DevName As Long
Dim DevName2 As Long
Dim RowNumber As Long
Dim RowNumberPlus1 As Long

'Define other variables
Dim NumOfBkrRows As Long
Dim NumOfBkrs As Long
Dim i As Long
Dim j As Long
Dim AColValue As String



'Set other variables appropriately
RowNumber = 6
NumOfBkrRows = 0
NumOfBkrs = 0
i = 0
j = 0
AColValue = Range("A6").Value

'Find number of breakers
Do While j <> 1
    If AColValue = "LV Fuses" Then 'Fuse info starts with a row labelled "LV Fuses" in Col A
    j = 1
    Exit Do
    ElseIf AColValue = "HV/MV with Trip-Unit" Then '
    j = 1
    Exit Do
    ElseIf AColValue = "HV/MV without Trip-Unit" Then '
    j = 1
    Exit Do
    ElseIf AColValue = "Relays" Then '
    j = 1
    Exit Do
    ElseIf AColValue = "MCP" Then '
    j = 1
    Exit Do
    ElseIf AColValue = "MOL" Then '
    j = 1
    Exit Do
    ElseIf AColValue = "HV Fuses" Then '
    j = 1
    Exit Do
    ElseIf AColValue = "Switches" Then '
    j = 1
    Exit Do
    ElseIf AColValue = "" Then '
        RowNumberPlus1 = RowNumber + 1
        End If
        AColValue = Range("A" & RowNumberPlus1)
        If AColValue = "" Then
            RowNumberPlus1 = RowNumberPlus1 + 1
            AColValue = Range("A" & RowNumberPlus1)
            End If
            If AColValue = "" Then
                j = 1
                End If
                Exit Do


    NumOfBkrRows = NumOfBkrRows + 1
    RowNumber = RowNumber + 1
    AColValue = Range("A" & RowNumber)
Loop
NumOfBkrs = NumOfBkrRows / 4

【问题讨论】:

  • 要在 StackOverflow 中获得有用的答案,请务必发布您正在谈论的代码,否则我们无法真正帮助您。
  • 对不起,伙计们,我没有意识到我在没有代码的情况下发布了问题。 @MarkusAppel
  • 代码中的任何内容都不会更改RowNumber 的值。
  • @SJR 还有其他代码可以更改 RowNumber 的值,当我调试它时,它工作正常。我遇到的问题是 RowNumberPlus1 = RowNumber + 1。当我将鼠标悬停在 RowNumberPlus1 上时,该变量的值变为 0,即使根据左侧的表达式它应该为 1 (RowNumber + 1)
  • blahblah = RowNumberPlus1 + RowNumber -- 什么是blahblah?如果您提供minimal reproducible example(强调最小),这将有所帮助。我无法实际运行您的代码。就悬停而言——您可能会看到变量的值 before 表达式被评估,而不是之后。在调试模式下,很容易在这一点上感到困惑。

标签: excel vba


【解决方案1】:

你在循环的底部有一个无条件的Exit Do(注意它出现在最后一个End If之后)。这导致循环在遇到 RowNumber = RowNumber + 1 行之前终止。

您的代码中很可能还有其他错误。如果是这样,修复缩进将是开始所需调试的好地方。我怀疑条件实际上并没有像您想象的那样相互嵌套。如果你想自动修复它,内置的 VBA 编辑器没有自动缩进功能,但 Rubberduck 插件有

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2016-02-24
    • 2010-10-15
    相关资源
    最近更新 更多