【问题标题】:How do i locate the variable that is the mismatch (Run-time error 13)如何找到不匹配的变量(运行时错误 13)
【发布时间】:2019-10-14 11:27:44
【问题描述】:

我正在尝试为我的同事 VBA 代码找到此代码中的变量不匹配。但是我一直无法找到问题所在。该代码应该根据第三张表中的手动输入更新 2 张不同的表。这是关于安全隐患的。

调试说这行代码搞砸了

 Previouscellcontentbefore = ActiveCell.Offset(rowbefore + 1, columbefore + 1)

完整代码:

Dim I As Integer
Dim row As Integer
Dim before As String
Dim after As String
Dim cons As String
Dim conscat As String
Dim checks As String
Dim check2 As String
Dim check3 As String
Dim rowbefore As String
Dim columbefore As String
Dim rowafter As String
Dim columafter As String
Dim checkbefore As String
Dim checkafter As String
Dim Previouscellcontentbefore As Integer
Dim Previouscellcontentafter As Integer



Sheets("for calculations").Visible = True

cons = Application.InputBox(prompt:="Personnel; Environment; Assets; Reputation; All", Title:="Choose consequence (NB: Case sensitive)", Default:="All")
Worksheets("For calculations").Activate

Range("D37:I42").ClearContents
Range("L37:Q42").ClearContents
Range("C34").ClearContents

Select Case cons

    Case "All"
        Range("C34").Value = "Risk matrix shows all types of consequences"
    Case "Personnel"
        Range("C34").Value = "Risk matrix shows all types of Personnel consequences"
    Case "Environment"
        Range("C34").Value = "Risk matrix shows Environmental consequences"
    Case "Asset"
        Range("C34").Value = "Risk matrix shows Asset consequences"
    Case "Reputation"
        Range("C34").Value = "Risk matrix shows Reputation consequences"
End Select


For I = 1 To 200
    Range("C47").Value = Worksheets("HAZIDS").Cells(I + 5, 2).Value
    conscat = Range("F47")
    check2 = cons Like conscat
    check3 = cons Like "All"

If cons Like "All" Then
    check2 = True
End If

    If check2 Then

before = Range("D47")
after = Range("E47")
rowbefore = Mid(before, 2, 1)
columbefore = Mid(before, 4, 1)
rowafter = Mid(after, 2, 1)
columafter = Mid(after, 4, 1)

checkbefore = Not rowbefore Like "" And Not columbefore Like ""
checkafter = Not rowafter Like "" And Not columafter Like ""

    If checkbefore Then
        Range("C36").Select
        Previouscellcontentbefore = ActiveCell.Offset(CInt(rowbefore) + 1, CInt(columbefore) + 1)
        ActiveCell.Offset(CInt(rowbefore) + 1, CInt(columbefore) + 1) = Range("C47").Value & ", " & Previouscellcontentbefore

    If checkafter Then
        Range("K36").Select
        Previouscellcontentafter = ActiveCell.Offset(CInt(rowafter) + 1, CInt(columafter) + 1)
          ActiveCell.Offset(CInt(rowafter) + 1, CInt(columafter) + 1) = Range("C47").Value & ", " & Previouscellcontentafter

        End If
    End If
End If


End Sub

我希望宏根据“HAZIDS”中的手动输入更新工作表“之前的风险矩阵”和“之后的风险矩阵” 然而,“用于计算”的工作表似乎有问题

【问题讨论】:

  • 这里很多事情都可能出错。使用Option Explicit 并请完整声明您的变量。 rowbefore + 1,rowbefore 应该很长,但是您在其中存储了一个字符串? rowbefore = Mid(before, 2, 1)?
  • 如果你打算将它用于“加法”,那么就声明它。这将复制问题并强调正确的变量声明。 Dim rw As Long: rw = "Sid"
  • 它是字符串的原因是由于字符串在“计算”表中具有预设值。因此它连接字符串。
  • 然后将它们转换为 long,然后再将其用于添加。这样你就会知道它们中是否有任何无效值...

标签: excel vba mismatch


【解决方案1】:

你在做

rowbefore = Mid(before, 2, 1) 'Rownumber in matrix before
columbefore = Mid(before, 4, 1) 'Columnumber in matrix before

由于您没有声明这些变量,它们会得到一个字符串值。 (因为Mid返回一个字符串)

之后在调试指示的行中执行以下操作:

Previouscellcontentbefore = ActiveCell.Offset(rowbefore + 1, columbefore + 1)

您将整数值 1 添加到字符串值,这没有意义。

您需要做的是使用option explicit 并正确定义您的变量。 然后你可能需要像这样使用函数 Cint 将字符串转换为整数

Previouscellcontentbefore = ActiveCell.Offset(Cint(rowbefore) + 1, Cint(columbefore) + 1)

这仅在您的rowbeforecolumnbefore 实际上包含可以转换为整数的字符串时才有效。如果没有,你会得到一个错误。

【讨论】:

  • 我现在已经定义了我的所有变量(正如我所期望的那样,我正在搜索工作表。这不是我的 Excel 工作表,只是试图修复它。)并稍微更新了代码,虽然现在我收到一个错误“for I = 1 to 200 can't be executed without "next"
  • 然后在循环末尾添加next 语句。如果您无法弄清楚,并且您已经无法在此处找到答案,请在新问题中发布一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
相关资源
最近更新 更多