【发布时间】:2020-07-19 12:33:03
【问题描述】:
我有一个带有 2 个组合框的用户窗体,它应该从外部工作簿获取数据范围。 我正在使用 F8 单步执行代码以查看该过程,一切运行都没有错误,但是当用户窗体显示时 ComboBoxes 为空。
当我在 Watch 窗口中检查命名范围时,命名范围已设置并有效。 当我将命名范围设置为组合框时,我不确定 userform_initialize 是否存在问题?
任何建议将不胜感激。
Option Explicit
Public m_Cancelled As Boolean
Public Const myRangeNameVendor As String = "myNamedRangeDynamicVendor"
Public Const myRangeNameVendorCode As String = "myNamedRangeDynamicVendorCode"
Public VendorName As String
Public VendorCode As String
Sub NamedRanges(wb As Workbook, wSh As Worksheet)
'declare variables to hold row and column numbers that define named cell range (dynamic)
Dim myFirstRow As Long
Dim myLastRow As Long
'declare object variable to hold reference to cell range
Dim myNamedRangeDynamicVendor As Range
Dim myNamedRangeDynamicVendorCode As Range
'identify first row of cell range
myFirstRow = 2
'Vendor Name range
With wSh.Cells
'find last row of source data cell range
myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'specify cell range
Set myNamedRangeDynamicVendor = .Range(.Cells(myFirstRow, "A:A"), .Cells(myLastRow, "A:A"))
End With
'create named range with workbook scope. Defined name is as specified. Cell range is as identified, with the last row being dynamically determined
wb.Names.Add Name:=myRangeNameVendor, RefersTo:=myNamedRangeDynamicVendor
'Vendor Code range
With wSh.Cells
'specify cell range
Set myNamedRangeDynamicVendorCode = .Range(.Cells(myFirstRow, "B:B"), .Cells(myLastRow, "B:B"))
End With
'create named range with workbook scope. Defined name is as specified. Cell range is as identified, with the last row being dynamically determined
wb.Names.Add Name:=myRangeNameVendorCode, RefersTo:=myNamedRangeDynamicVendorCode
End Sub
' Returns the textbox value to the calling procedure
Public Property Get Vendor() As String
VendorName = cboxVendorName.Value
VendorCode = cboxVendorCode.Value
End Property
Private Sub buttonCancel_Click()
' Hide the Userform and set cancelled to true
Hide
m_Cancelled = True
End Sub
' Hide the UserForm when the user click Ok
Private Sub buttonOk_Click()
Hide
End Sub
' Handle user clicking on the X button
Private Sub FrmVendor_QueryClose(Cancel As Integer, CloseMode As Integer)
' Prevent the form being unloaded
If CloseMode = vbFormControlMenu Then Cancel = True
' Hide the Userform and set cancelled to true
Hide
m_Cancelled = True
End Sub
Private Sub FrmVendor_Initialize()
'add column of data from spreadsheet to your userform ComboBox
With Me
cboxVendorName.RowSource = ThisWorkbook.Names(Split(.Tag, "|")(0)).Address(external:=True)
cboxVendorCode.RowSource = ThisWorkbook.Names(Split(.Tag, "|")(1)).Address(external:=True)
End With
cboxVendorCode.ColumnCount = 2
End Sub
Sub Macro5()
Dim wb As Workbook
Dim ws As Worksheet
Dim path As String
Dim MainWB As Workbook
Dim MasterFile As String
Dim MasterFileF As String
Set ws = Application.ActiveSheet
Set MainWB = Application.ActiveWorkbook
Application.ScreenUpdating = False
'Get folder path
path = GetFolder()
MasterFile = Dir(path & "\*Master data*.xls*")
MasterFileF = path & "\" & MasterFile
'Check if workbook open if not open it
If Not wbOpen(MasterFile, wb) Then
Set wb = Workbooks.Open(MasterFileF, False, True)
End If
'Set Vendor Name and Code Range names
Call NamedRanges(wb, wSh)
'Select Vendor name and Vendor code with User Form and set variables
' Display the UserForm
With New FrmVendor
.Tag = myRangeNameVendor & "|" & myRangeNameVendorCode
.Show
End With
VendorName = FrmVendor.cboxVendorName.Value
VendorCode = FrmVendor.cboxVendorCode.Value
' Clean up
Unload FrmVendor
Set FrmVendor = Nothing
【问题讨论】:
-
整合对
VendorName =...和VendorCode =...的结束赋值到With New FrmVendor构造中(在.Show和End With之后),但简单地省略引用用户窗体的默认实例FrmVendor:例如VendorName = .cboxVendorName.Value(注意写点前缀)。因此,您正在引用当前的“新”实例,而不是在设计时可能输入的值。 - 在AddItem not populating options in combobox 找到更多解释 -
顺便说一句,您无需进行整个清理工作,因为新用户窗体实例的生命周期会自动以
End With结束。旁注:通常对FrmVendor的任何进一步调用都会重新激活您尝试销毁的默认实例,尽管它没有在您的情况下使用:-) -
@T.M.我尝试将代码更改为您的第一个建议。组合框为空时,我仍然遇到同样的问题。我不确定移动这两个参数是否有助于组合框不填充范围,因为它们只是为了捕获在框中选择的选定值(当它们最终填充时)。不过,谢谢,任何其他建议都会有很大帮助。我以前从未使用过用户表单和组合框,所以在代码中苦苦挣扎。
-
假设之前也有问题(见聊天)-tldr:-;可能下面的答案加上我的提示会帮助你:-)
-
绝对命名约定是
UserForm_Initialize(),无论表单名称是什么,即不是FrmVendor_Initialize();类似UserForm_QueryClose(Cancel As Integer, CloseMode As Integer):-)
标签: excel vba combobox range userform