我正在尝试为我的同事VBA代码查找此代码中的变量不匹配。但是我还没有找到问题所在。该代码应该根据第三张纸上的手动输入来更新2张不同的纸。这是关于安全隐患的。
调试说这行代码是一团糟
Previouscellcontentbefore = ActiveCell.Offset(rowbefore + 1, columbefore + 1)
看着电池,应该是字符串,不应该有不匹配的地方。
Dim row, I As Integer
Dim cons As String
'Application.ScreenUpdating = False
Sheets("for calculations").Visible = True
'cons = Application.InputBox(prompt:="Personnel = Personnel; Environment = Environment; Asset = Asset; Reputation = Reputation; All = ALL", Title:="Choose consequence (NB: Case sensitive)", Default:="ALL")
cons = Application.InputBox(prompt:="Personnel; Environment; Asset; 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 Environmetal consequences"
Case "Asset"
Range("C34").Value = "Risk matrix shows Asset consequences"
Case "Reputation"
Range("C34").Value = "Risk matrix shows Reputation consequences"
End Select
'MsgBox ("Cons = " & Cons & " " & "ConsCat=" & conscat)
For I = 1 To 200
'Application.ScreenUpdating = False
'Worksheets("For calculations").Activate
'Sheets("for calculations").Visible = True
Range("C47").Value = Worksheets("HAZIDS").Cells(I + 4, 2).Value
conscat = Range("F47").Text 'Consequence category
check2 = cons Like conscat 'Check2 is true if the actual HAZID (no. I) has the same consequence category as pomted in the input box
check3 = cons Like "All"
If cons Like "All" Then
check2 = True
End If
'MsgBox (check2 & " Cons = " & cons & " Conscat = " & conscat)
If check2 Then 'Insert only HAZID in risk matrices if then have the considered consequence type
before = Range("D47").Text
after = Range("E47").Text
rowbefore = Mid(before, 2, 1) 'Rownumber in matrix before
columbefore = Mid(before, 4, 1) 'Columnumber in matrix before
rowafter = Mid(after, 2, 1) 'Rownumber in matrix after
columafter = Mid(after, 4, 1) 'Columnumber in matrix after
Checkbefore = Not rowbefore Like "" And Not columbefore Like "" 'Check if risk level before is evaluated
Checkafter = Not rowafter Like "" And Not columafter Like "" 'Check if risk level after is evaluated
'MsgBox ("Rowbefore = " & rowbefore & " Columbefore = " & columbefore & " " & "Checkbefore = " & Checkbefore & " Checkafter = " & Checkafter)
If Checkbefore Then 'Write HAZID no. in appropriate cell in risk before matrix
'MsgBox ("Efter then Rowbefore = " & rowbefore & " Columbefore = " & columbefore & " " & "Before = " & before & " " & check)
Range("C36").Select
Previouscellcontentbefore = ActiveCell.Offset(rowbefore + 1, columbefore + 1)
ActiveCell.Offset(rowbefore + 1, columbefore + 1) = Range("C47").Value & ", " & Previouscellcontentbefore
If Checkafter Then 'Write HAZID no. in appropriate cell in risk before matrix
Range("K36").Select
Previouscellcontentafter = ActiveCell.Offset(rowafter + 1, columafter + 1)
ActiveCell.Offset(rowafter + 1, columafter + 1) = Range("C47").Value & ", " & Previouscellcontentafter
End If
Else
'MsgBox ("After else - no action " & check)
'Exit For
End If
Else
'MsgBox ("Rowbefore = " & rowbefore & " Columbefore = " & columbefore)
End If
'Worksheets("Risk Matrix Before").Activate
'Application.ScreenUpdating = True
'Sheets("for calculations").Visible = True
Next I
Sheets("for calculations").Visible = False
Worksheets("Risk Matrix Before").Activate
我希望宏根据“ HAZIDS”中的手动输入更新工作表“之前的风险矩阵”和“之后的风险矩阵”然而,“用于计算”表似乎有错误]
您正在做
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)
仅当您的rowbefore
和columnbefore
实际包含可以转换为整数的字符串时,此选项才有效。如果没有,您将得到一个错误。