在一个组合框中切换两个动态范围时要求 vba 代码

Asking for vba codes in switching two dynamic ranges in one combobox

亲爱的,

我正在寻找 vba 在一个组合框中切换两个动态范围的代码,我在 A 列下设置一个范围(例如 1,2,3,4,5),在 D 列下设置另一个范围( a、b、c、d、e)。并且有两个命令按钮(“字母”和“数字”)和一个组合框。当我点击“字母”按钮时,组合框的范围将是字母;当我单击“数字”按钮时,组合框的范围将为数字。我的代码如下:

Private Sub Cmd_letter_Click()
Dim x As String
x = "d1"
numberorletter.Clear
Call numberorletterprocedure
End Sub

Private Sub Cmd_number_Click()
Dim x As String
x = "a1"
numberorletter.Clear
Call numberorletterprocedure
End Sub

Private Sub numberorletterprocedure()
Dim x As String
Dim numberorletter As Range
Dim ws As Worksheet
Set ws = sheet1
Set numberorletter.List = sheet1.Range(x, sheet1.Range(x).End(xlDown)).Value
End Sub

然而,它不起作用,你能告诉我我哪里出错了吗?如果它是通过创建一个函数而不是过程来完成的,它会是什么样子?谢谢!

试试这个

Private Sub Cmd_letter_Click()
Dim x As String
x = "d1"
Call numberorletterprocedure(x)
End Sub

Private Sub Cmd_number_Click()
Dim x As String
x = "a1"
Call numberorletterprocedure(x)
End Sub

Private Sub numberorletterprocedure(x As String)
Dim ws As Worksheet
Set ws = Sheet1
'* assuming your combobox name is numberorletter
'* if not change it to the correct name
numberorletter.List = ws.Range(x, ws.Range(x).End(xlDown)).Value
End Sub