我非常擅长VBA for excel,目前正在尝试在我的工作簿中实现一个组合框。
我有一个很大的工作表,其中包含许多工作表,其中一个工作表有一个指南列表'名字,我在那里选择它们(" A2:A80")并命名为" Guides_names"。 我还添加了一个vba宏(在模块中),可以从工作簿中的任何位置激活,以显示列表:
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Guides_names"
.IgnoreBlank = True
.InCellDropdown = False
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
并且效果很好:它在我选择的任何单元格中实现列表并打开它,然后选择将显示在单元格中。 但: 1. lise中的名字字体非常小; 2.没有自动补充。
所以我决定使用它并隐藏验证列表并添加组合框。我发现了很多例子,并设法在其中一张纸上添加了一个漂亮的组合框,它完全打开,同时双击任何单元格我在工作表内单击 但是当我在一个模块中实现类似的代码时:我无法自动打开列表(问题在于Me.TempCombo.DropDown:它不知道ME),并且在从中选择一个值之后列出组合框 继续显示, 当我尝试从另一张纸上激活它时,它根本不会工作......
感谢您提供任何帮助或建议!
Sub Select_Guide()
Dim str As String
Dim cboTemp As OLEObject
Dim rngSelectedRange As Range
Dim ws As Worksheet
Set ws = ActiveSheet
' Here i use the original code:
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Guides_Names"
.IgnoreBlank = True
.InCellDropdown = False
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Set rngSelectedRange = Range(ActiveCell, ActiveCell.Offset(numRows, numCols))
Set cboTemp = Worksheets("dbdb").OLEObjects("TempCombo")
On Error Resume Next
With cboTemp
'clear and hide the combo box
.ListFillRange = "Guides_Names"
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If rngSelectedRange.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = rngSelectedRange.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = rngSelectedRange.Left
.Top = rngSelectedRange.Top
.Width = rngSelectedRange.Width + 5
.Height = rngSelectedRange.Height + 5
.ListFillRange = str
.LinkedCell = rngSelectedRange.Address
End With
cboTemp.Activate
'open the drop down list automatically
' Me.TempCombo.DropDown - worked only when called by double_click function
TempCombo.DropDown ' This doesn't really opens the combo box
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub