vb如何將子程式的值傳遞出來,vb 陣列在子程式裡傳遞的問題。求助

2022-01-05 02:06:48 字數 3394 閱讀 4138

1樓:匿名使用者

有兩種方法可以實現將子程式中的值傳遞出來。

(1)利用模組級或全域性變數。例如

option explicit

' r是一個模組級變數

dim r as integer

sub add(byval x as integer, byval y as integer)

'將計算結果存放到 r

r = x + y

end sub

private sub form_click()'呼叫子程式add

call add(10, 20)

'獲取計算結果並列印

print r

end sub

(2)用 byref 引數實現計算結果回傳。 例如option explicit

'add子程式利用 byref r as integer 回傳值sub add(byref r as integer, byval x as integer, byval y as integer)

r = x + y

end sub

private sub form_click()dim r as integer

'呼叫子程式add,計算結果回傳到變數 rcall add(r, 10, 20)

'列印結果

print r

end sub

2樓:匿名使用者

function aaaa() as integer

aaa=2 '這裡是返回值,也就是 左側是函式名,右側是你想要返回的值

end function

3樓:**大師兄

vb中函式的引數都是預設傳址的,也就是說你主程式裡的引數在子過程裡面的變化都會反映到主程式中去,也可以通過函式的返回值來使主程式獲取子程式裡面產生的值

4樓:匿名使用者

vb的子程式的引數,分兩種:

用關鍵字byval 宣告的是傳值引數

用關鍵字byref 宣告的是傳址引數

例如子程式 aaaa:

sub aaaa(vyval a as integer,byref b as integer)

a=99

b=100

end sub

現在去呼叫它如下:

dim x as integer,y as integerx=1y=2

print x,y

aaaa x,y

print x,y

可以,看到兩次輸出的結果x相同,y不同

y把子程式的結果傳遞出來了

當然,編寫函式可以把結果傳遞出來,但是函式名只可以傳遞一個結果,而利用byref可以傳遞多個結果.

5樓:

寫的子程式是函式的話,就可以了

比如:function say(byval s as string)msgbox s

say="ok"

end function

vb 陣列在子程式裡傳遞的問題。求助

6樓:匿名使用者

dim max as integer '未將max變數定義全域性private sub command1_click()dim n(1 to 10) as integer, s as string

for i = 1 to 10

n(i) = int(rnd * (200 - 100 + 1) + 100)

s = s & n(i) & vbcrlfnext i

text1.text = s

call procmax(n)

label1.caption = max

end sub

private sub procmax(byref a() as integer)

max = a(lbound(a))

for i = lbound(a) to ubound(a)if a(i) > max then

max = a(i)

end if

next i

label1.caption = max

end sub

vb 怎麼 在自定義函式中,返回多個值?請舉例說明

7樓:匿名使用者

sub 按鈕1_click()

dim 周長 as double, 面積 as double, 半徑 as double

周長 = 0

面積 = 0

半徑 = 15#

call subn(周長, 面積, 半徑)msgbox 周長 & " " & 面積

end sub

sub subn(byref 周長引數, byref 面積引數, byval 半徑引數)

if 半徑引數 < 0 then

周長引數 = 0

面積引數 = 0

else

周長引數 = 2 * 3.14 * 半徑引數面積引數 = 3.14 * 半徑引數 * 半徑引數end if

end sub

以上**是在excel的vba中除錯的,可以滿足的要求。

8樓:匿名使用者

不定義全域性變數,根本不可能做到的你的要求。

你用sub就必須要定義全域性公有變數。

★:專業定製各種小軟體,小型資料管理作業系統★:包售後 1 年(不含新增功能),6×18小時服務。實時解決問題!

excel vba呼叫子程式時如何傳遞引數

9樓:zzllrr小樂

vba呼叫子程式時,如果不帶引數,

直接寫sub過程名,或者call sub名稱即可。

如果需要傳遞引數專:屬

同樣可以使用call:

例如:call picincomment(1, 250)引數寫在後面,不帶括號:

例如:picincomment 1, 250也可以賦值給其他變數:

例如:result = picincomment(1, 250)

10樓:匿名使用者

用法有三種

1、賦值給變數就要帶括號 比如 a = picincomment (1, 250)

2、call picincomment (1, 250)3、picincomment 1, 250

11樓:明佳妙明

sub picincomment(optional byval picreserve as integer, optional byval widthedge as integer )

12樓:匿名使用者

用 call picincomment(1, 250)

如何將js中的值傳遞給,如何將js中的值傳遞給html

js方式實現靜態頁之間值傳遞,參考如下 在body標籤之間加此行 然後,新建a.html新頁,同樣在body標籤之間加此行 如下 var value www.sucaijiayuan.com var odiv document.getelementbyid div odiv.innerhtml va...

如何用子程式求數的最大值,如何用子程式求三個數的最大值

假設 三個數為 3 6 9 彈出訊息框 顯示 最大值 6 程式執行正常 如下 望採納 最大值函式 function max data as string as integer dim group dim i as integer dim j as integer group split data,f...

VB2019中如何將某一畫素點的R G B值分別提取出來

private sub picturebox1 mousemove byval sender as object,byval e as system.windows.forms.mouseeventargs handles picturebox1.mousemove if me.picturebox...