2011-04-25 41 views
1

之外的变量我试图从我的程序callLum()中调用子Lum(),要求Lum分别获得值11,12,13,但看起来像Lum无法识别我之前在callLum()中定义的计数器,如下面的脚本所示。原因是在我的实际Main脚本中,我有一个长For-next循环,如果我不打断Msgbox的循环,程序似乎在到达“next”时找不到“For”字样。这就是为什么我最终包装了主要脚本,后来创建了一个简单的子程序来调用它。Excel-VBA - 调用Sub并要求它调用定义在Sub

Sub callLum() 
    Dim counter As Integer 
    Dim LR As Long, j As Long 

    counter = 10 
    'checking number of rows in column A 
    LR = Range("A" & Rows.Count).End(xlUp).Row 

    For j = 1 To LR 
    If Not IsEmpty(Range("A" & j)) And Not IsEmpty(Range("B" & j)) Then _ 
    counter = counter + 1   
    Call Lum                
    Next j 
    MsgBox "THE END" 
End Sub 


Sub Lum() 
    MsgBox "Counter value is " & counter 
End Sub 

回答

2

变化绥为:

Sub Lum(ByVal counter as Integer) 
    MsgBox "Counter value is " & counter 
End Sub 

对于你的循环,而不是单独测试范围的每一个元素,指定范围作为AX:其中x和y是由填充在Ay的你的循环。

+0

它的工作原理,当我更改“呼叫Lum”到“呼叫Lum(byval计数器)”在callLum子也,谢谢。 – 2011-04-27 03:33:19