所以我知道一次又一次询问这个错误,但是我找不到一个可以重复出现的问题。
以下内容在PERSONAL.XLSB工作簿中进行了声明,因此我可以轻松地在不同的项目上使用该宏。当我在for循环中添加第二个If块时,错误开始弹出,请注意,在上述添加之前,该宏按预期工作。
Sub AddNote()
Dim Dict As New Scripting.Dictionary
Dict.Add "101-104", "Includes 101, 102, 103, 104"
Dict.Add "061, 071", "Includes 061, 071"
Dict.Add "076, 077, 081", "Includes 076, 077, 081"
Dict.Add "111, 112, 113, 221, 222", "Includes 111, 112, 113, 221, 222"
Dict.Add "111, 112, 221, 222", "Includes 111, 112, 221, 222"
Dict.Add "111-115, 221, 222", "Includes 111, 112, 113, 114, 115, 221, 222"
Dict.Add "101-104 Early", "Includes 101, 102, 103, 104"
Dict.Add "101-104 Late", "Includes 101, 102, 103, 104"
Dict.Add "101-104 Mid", "Includes 101, 102, 103, 104"
Dict.Add "111-115, 221, 222 Early", "Includes 111, 112, 113, 114, 115, 221, 222"
Dict.Add "111-115, 221, 222 Late", "Includes 111, 112, 113, 114, 115, 221, 222"
Dict.Add "161-164", "Includes 161, 162, 163, 164"
Dict.Add "161-164 Early", "Includes 161, 162, 163, 164"
Dict.Add "161-164 Late", "Includes 161, 162, 163, 164"
Dict.Add "131, 132", "Includes 131, 132"
Dict.Add "062, 064, 066-068", "Includes 062, 064, 066, 067, 068"
Dict.Add "078, 104, 105-107", "Includes 078, 104, 105, 106, 107"
Dict.Add "104, 108, 121", "Includes 104, 108, 121"
Dict.Add "231, 241, 242", "Includes 231, 241, 242"
Dict.Add "072, 074", "Includes 072, 074"
Dict.Add "231, 241, 242 Early", "Includes 231, 241, 242"
Dict.Add "231, 241, 242 Late", "Includes 231, 241, 242"
Dict.Add "114, 115", "Includes 114, 115"
Dim Rng As Range
Dim ws1 As Worksheet
Set ws1 = Sheets("BY HUNT")
Set Rng = ws1.Range("A2:A162")
For Each cell In Rng
If Dict.Exists(VBA.Trim(cell)) Then
ws1.Range("AA" & cell.Row).Value = Dict.Item(VBA.Trim(cell.Value))
End If
If VBA.Trim(cell.Value) Like "*Early*" Then
wsl.Range("Z" & cell.Row).Value = "Early"
ElseIf VBA.Trim(cell.Value) Like "*Late*" Then
wsl.Range("Z" & cell.Row).Value = "Late"
ElseIf VBA.Trim(cell.Value) Like "*Mid*" Then
wsl.Range("Z" & cell.Row).Value = "Mid"
Else
wsl.Range("Z" & cell.Row).Value = "All"
End If
Next cell
End Sub
我对VBA并没有过多的经验,但是我确实阅读了我在这里发现的与我的问题有关的其他问题,但都没有取得成果。我不明白为什么我可以在第一个if语句中弄乱值,但在第二个中却得到标题中提到的错误。调试指向else子句中的行,如果删除该行,则调试仅指向第二个if块中的另一行。任何方向将不胜感激。
答案 0 :(得分:3)
将Option Explicit
添加到模块顶部并重新编译。您的变量名中有拼写错误。 ws1
与wsl