简体   繁体   English

VBA Excel-通过工作表和范围作为参数

[英]VBA Excel - Pass Worksheet and Range as Parameters

Can anyone help me convert a Sub into a function so I could use it to a different form controls? 谁能帮助我将Sub转换为函数,以便可以将其用于其他表单控件? I'm new to VBA excel and I've been trying to convert this for hours with no luck. 我是VBA excel的新手,并且我一直在尝试将其转换几个小时而没有运气。 Appreciate it if someone could help. 如果有人可以帮助,请多加赞赏。

Private Sub ComboBox1_Change()
    Dim v, e
    With Sheets("DATABASE").Range("minRange").SpecialCells(2)
        v = .Value
    End With

    With CreateObject("scripting.dictionary")
        .comparemode = 1
        For Each e In v
        If Not .exists(e) Then .Add e, Nothing
        Next
        If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
    End With
End Sub

Thanks. 谢谢。

A range variable should have the worksheet already qualified so no need to pass that separately. 范围变量应该已经对工作表进行了限定,因此无需单独传递它。 Just write your sub to accept a range argument: 只需编写您的sub即可接受范围参数:

Sub MySub(ByVal rng As Excel.Range)

    MsgBox rng.Address

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM