2016-06-21 76 views
0

因此,我正在使用Excel 2010,現在,我試圖使用計算一個變量的值在一個子中,並希望它在多個子中使用,除了我可以不知道該怎麼做。這裏是在多個子VBA中使用變量

Public Sub Calc() 
Dim i As Integer 
i = 10 - 5  
End Sub 


'Other sub that will use the value for i calculated in the Calc sub 

Sub Macro() 
y = i +5 
End Sub 

我怎麼想,以能夠使用我使用的最基本的形式是什麼的一個例子/傳爲「我」在/該數值到子宏?

回答

1

插入新的模塊,並定義爲i

Public i As Integer 

然後你就可以,只要你想使用它。

如需進一步信息,請訪問:Scope of variables in Visual Basic for Applications

+0

................ Nice Reference ................ –

+0

@ Gary的學生,謝謝!乾杯,Maciej –

1

移動Dim上述潛艇:

Public i As Integer 

Public Sub Calc() 
    i = 10 - 5 
End Sub 

Sub Macro() 
    y = i + 5 
    MsgBox y 
End Sub 

Sub MAIN() 
    Call Calc 
    Call Macro 
End Sub 

嘗試運行MAIN()

+0

這是做的伎倆,謝謝! – bakeson

0

我想用函數代替子的。

Sub Main() 
    Msgbox Macro(calc) 
end Sub 

Function Calc() as Integer 
    Dim i As Integer 
    i = 10 - 5  
    Calc = i 
End Function 

Function Macro(i as Integer) as Integer 
    Macro = i + 5 
End Function