2011-04-25 42 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