簡體   English   中英

將值從子傳遞到Excel VBA中的函數

[英]Passing values from sub to function in Excel VBA

我不斷收到ByRef錯誤,但似乎無法弄清楚。 代碼全部寫在同一模塊中。

Sub GetTime(Labelname As Object)
Dim Hint, Mint, Sint As Integer
Dim time As String

Hint = CInt(Hour(Now))
Mint = CInt(Minute(Now))
Sint = CInt(Second(Now))

time = CorrectTime(Hint, Mint, Sint)

Private Function CorrectTime(Hours As Integer, Minutes As Integer, Seconds As Integer) As String
Dim HS, MS, SS As String

If Len(CStr(Hours)) = 1 Then
    HS = "0" & CStr(Hours)
Else
    HS = CStr(Hours)
End If

If Len(CStr(Minutes)) = 1 Then
    MS = "0" & CStr(Minutes)
Else
    MS = CStr(Minutes)
End If

If Len(CStr(Seconds)) = 1 Then
    SS = "0" & CStr(Seconds)
Else
    SS = CStr(Seconds)
End If

CorrectTime = HS & ":" & MS & ":" & SS
End Function

每當我嘗試運行代碼時,它都會給我一個錯誤

time = CorrectTime(Hint, Mint, Sint)

錯誤類型為ByRef不匹配。

我看不到能解決這個問題的什么?

常見陷阱: Dim Hint, Mint, Sint As Integer

這里只有Sint是Integer,其他兩個變量沒有聲明為類型,因此默認為Variant 當傳遞給期望整數的函數時,將引發不匹配錯誤。

糾正:

Dim Hint As Integer, Mint As Integer, Sint As Integer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM