2016-09-19 83 views
-4

当宏退出For循环时,出现类型不匹配错误。我不知道为什么,但希望得到任何帮助。我曾尝试寻找这个问题,但没有任何建议适用于我的情况。退出循环的Excel宏类型不匹配错误

Option Explicit 
     Dim MODULE_NAME() As String 
     Dim Counter As Long 
     Dim i As Long 
1210       For Counter = i To CLng(i + CLng(CLng(UBound(MODULE_NAME)) Mod 127)) 
1220        Call MacroLineNum(Counter, MODULE_NAME, TotalLines) 
1230        If Counter Mod UpdateFreq = 0 Then 
1240         '**Save the file 
1250         With gloMacroWorkbook 
1260          If Not .ReadOnly Then 
1270           .Save 
1280          Else 
1290           .Saved = False 
1300          End If 
1310         End With 
1320         LockWindowUpdate 0& 
1330         '------------------------------------------------------------------------------------------------- 
1340         ' Update the status bar. 
1350         '------------------------------------------------------------------------------------------------- 
1360         Call subStatusBarUpdater("Still updating the line numbers in the modules. " & Counter & " of " & UBound(MODULE_NAME) & " processed....") 
1370         If VBEHwnd Then 
1380          LockWindowUpdate VBEHwnd 
1390         End If 
1400        End If 
1410        If Counter = UBound(MODULE_NAME) Then 
1420         MsgBox "Exiting for loop." 
1430        End If 
1440       Next Counter 
1450       MsgBox "Saving file" 

我的代码行达到1420,而不是1450线甚至我加入类型转换功能“CLng函数”到1210,但似乎没有任何帮助。在1420和1450行之间,我总是得到'类型不匹配'的错误,没有详细说明在哪里以及为什么。

MODULE_NAME()数组是一个包含159个字符串条目的动态数组。任何帮助将不胜感激。

+0

'MacroLineNum'做什么? – Comintern

+0

@ Mat'sMug它跳跃10,所以它是145 :) –

+0

@AA它似乎你不是退出'For'循环,在行1430之后(在Next Counter之前)添加一个'MsgBox'并且写入'MsgBox “计数器是:”&计数器“ –

回答

0

我能够通过一些简单的步骤完全解决这个问题。我不知道为什么,但这是有效的。我只是添加了CLng呼叫,因为我无法让它早点工作。

Option Explicit 
    Dim MODULE_NAME() As String 
    Dim Counter As Long 
    Dim i As Long 
    Dim k As Long 
1200       k = i + (UBound(MODULE_NAME) Mod 127) 
1210       For Counter = i To k 
1230        Call MacroLineNum(Counter, MODULE_NAME, TotalLines) 
1240        If Counter Mod UpdateFreq = 0 Then 
1250         '**Save the file 
1260         With gloMacroWorkbook 
1270          If Not .ReadOnly Then 
1280           .Save 
1290          Else 
1300           .Saved = False 
1310          End If 
1320         End With 
1330         LockWindowUpdate 0& 
1340         '------------------------------------------------------------------------------------------------- 
1350         ' Update the status bar. 
1360         '------------------------------------------------------------------------------------------------- 
1370         Call subStatusBarUpdater("Still updating the line numbers in the modules. " & Counter & " of " & UBound(MODULE_NAME) & " processed....") 
1380         If VBEHwnd Then 
1390          LockWindowUpdate VBEHwnd 
1400         End If 
1410        End If 
1420        If Counter = UBound(MODULE_NAME) Then 
1430         MsgBox "Exiting for loop." 
1440        End If 
1450       Next Counter 
1460       MsgBox "Saving file" 

感谢大家的帮助和快速回复。尽管我想深究这一点,但根据要求创建测试代码已证明有点困难,因为我有太多互相连接的函数调用等,我需要将它们提取出来并提供测试代码。

+0

出于好奇,究竟是什么激励了1980年代VBA代码中的行号? –

+0

出于好奇,在VBA代码中具有1980年代行数的正确答案是什么? – ThunderFrame

+2

FWIW如果使用线路号码,例如'在错误转到1590'上,然后重新编号会引入令人讨厌的难以发现的错误。除了移植旧的传统QBASIC代码之外,VBA不需要行号;它们在新代码中是有害的,只是为了可读性。 –