传递对象时的byval vs byref

发布于 2025-01-24 18:20:21 字数 1222 浏览 1 评论 0原文

我想检查是否存在具有特定名称的工作表,因此我在下面生成Shtexist函数。 但是,对于Shtexist中的第二个参数。当我首先将其传递给byref时,shtexist(name,thisworkbook)表现良好,而shtexist(name,rwb)却没有,它显示了byref错误。 然后,我将其通过Byval,解决问题。 我的问题是,在这种情况下,为什么BYREF/BYVAL很重要?

Sub update_Click()
Dim updatelist
Dim relname, salname, insname, sname As String
Dim rwb, swb, iwb As Workbook
Dim year, month As Integer
updatelist = ThisWorkbook.Sheets("FrontPage").Range("u2", Range("u2").End(xlDown))
relname = Dir(ThisWorkbook.Path & "/" & "*关系表*.xls?")
Set rwb = Workbooks.Open(ThisWorkbook.Path & "/" & relname)
MsgBox (VarType(ThisWorkbook))
For Each i In updatelist
    sname = CStr(i)
    year = CInt(Left(sname, InStr(sname, ".") - 1))
    month = CInt(Mid(sname, InStr(sname, ".") + 1, 2))
    MsgBox (year & " " & month)
    If shtexist(sname, rwb) Then
        MsgBox ("yes")
    Else
        MsgBox ("no")
    End If
Next

End Sub

Function shtexist(name As String, Optional ByVal wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then
    Set wb = ThisWorkbook
End If
On Error Resume Next
    Set sht = wb.Sheets(name)
On Error GoTo 0
If sht Is Nothing Then
    shtexist = False
Else
    shtexist = True
End If
End Function

I want to check if a worksheet with particular name is exist, so I generate shtexist function below.
However, for the second parameter in shtexist. When I pass it byref at first, shtexist(name,thisworkbook) goes well while shtexist(name,rwb) does not and it shows byref error.
Then I pass it byval, problem solved.
My question is why byref/byval matters in this case?

Sub update_Click()
Dim updatelist
Dim relname, salname, insname, sname As String
Dim rwb, swb, iwb As Workbook
Dim year, month As Integer
updatelist = ThisWorkbook.Sheets("FrontPage").Range("u2", Range("u2").End(xlDown))
relname = Dir(ThisWorkbook.Path & "/" & "*关系表*.xls?")
Set rwb = Workbooks.Open(ThisWorkbook.Path & "/" & relname)
MsgBox (VarType(ThisWorkbook))
For Each i In updatelist
    sname = CStr(i)
    year = CInt(Left(sname, InStr(sname, ".") - 1))
    month = CInt(Mid(sname, InStr(sname, ".") + 1, 2))
    MsgBox (year & " " & month)
    If shtexist(sname, rwb) Then
        MsgBox ("yes")
    Else
        MsgBox ("no")
    End If
Next

End Sub

Function shtexist(name As String, Optional ByVal wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then
    Set wb = ThisWorkbook
End If
On Error Resume Next
    Set sht = wb.Sheets(name)
On Error GoTo 0
If sht Is Nothing Then
    shtexist = False
Else
    shtexist = True
End If
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-01-31 18:20:21

http://wwwwww.cpearson.com/excel/excel/excel/byrefbyval.aspx > byref vs byval传递对象时。但是,如果您通过thisworkbookrwb(只要它分配给某物)有任何区别 - 无论哪种情况,wb内部shtexist都没有分配分配。

您的声明可能是rwb(作为变体,因为每个变量都需要类型;您不仅将类型添加到行中的最后一个)

dim dim rwb作为工作簿,SWB作为工作簿,IWB作为Workbook

在VBA中声明变量: https://learn.microsoft.com/en-us/office/office/vba/vba/langueage/concepts/getting-started/declaring-declaring-variables-variables#:~: %20DECLARE%20个差异%20Variables%20in%20个%20statement。%20TO%20 specify%20A%20A%20DATA%20 type%20 type%2C%20you%20-20must%20must%20 include%20%20DATA%20DATA%20DATA%20 type%20 type%20 for%20 eht each%20vArable >。

http://www.cpearson.com/excel/byrefbyval.aspx explains ByRef vs ByVal when passing objects. However if you pass ThisWorkbook or rwb (as long as it's assigned to something) ByVal/ByRef shouldn't make any difference - in either case there's no assignment to wb inside shtexist so there should be no side-effects either way.

The issue is likely with your declaration of rwb (as Variant, since every variable needs a type; you don't just add the type to the last one in the line)

Dim rwb As Workbook, swb As Workbook, iwb As Workbook

Declaring variables in VBA: https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/declaring-variables#:~:text=You%20can%20declare%20several%20variables%20in%20one%20statement.%20To%20specify%20a%20data%20type%2C%20you%20must%20include%20the%20data%20type%20for%20each%20variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文