Excel 在工作簿中添加自动填充组合框

Excel 在工作簿中添加自动填充组合框,excel,combobox,vba,Excel,Combobox,Vba,我目前遇到的一个问题是,我的代码设置要求在工作表中存在数据验证的所有单元格中显示一个组合框。这对于一个工作表非常有效,但我希望在我的工作簿中的整个工作表中都能做到这一点。这是我目前正在运行的代码,我想知道是否有任何快速解决我目前面临的问题的方法。谢谢 '========================== Private Sub Worksheet_BeforeDoubleClick _ (ByVal Target As Range, _ Cancel As Boolean) Dim

我目前遇到的一个问题是,我的代码设置要求在工作表中存在数据验证的所有单元格中显示一个组合框。这对于一个工作表非常有效,但我希望在我的工作簿中的整个工作表中都能做到这一点。这是我目前正在运行的代码,我想知道是否有任何快速解决我目前面临的问题的方法。谢谢

'==========================
Private Sub Worksheet_BeforeDoubleClick _
  (ByVal Target As Range, _
    Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject

Dim ws As Worksheet
Set ws = Sheets("Test")

Set cboTemp = ws.OLEObjects("TempCombo")
  On Error Resume Next
  With cboTemp
  'clear and hide the combo box
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
  End With
On Error GoTo errHandler
  If Target.Validation.Type = 3 Then
    'if the cell contains
      'a data validation list
    Cancel = True
    Application.EnableEvents = False
    'get the data validation formula
    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    With cboTemp
      'show the combobox with the list
      .Visible = True
      .Left = Target.Left
      .Top = Target.Top
      .Width = Target.Width + 5
      .Height = Target.Height + 5
      .ListFillRange = str
      .LinkedCell = Target.Address
    End With
    cboTemp.Activate
    'open the drop down list automatically
    Me.TempCombo.DropDown
  End If

errHandler:
  Application.EnableEvents = True
  Exit Sub

End Sub
'=========================================
Private Sub TempCombo_LostFocus()
  With Me.TempCombo
    .Top = 10
    .Left = 10
    .Width = 0
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Value = ""
  End With
End Sub
 '====================================

Private Sub TempCombo_KeyDown(ByVal _
     KeyCode As MSForms.ReturnInteger, _
     ByVal Shift As Integer)
  Select Case KeyCode
    Case 9 'Tab
      ActiveCell.Offset(0, 1).Activate
    Case 13 'Enter
      ActiveCell.Offset(1, 0).Activate
    Case Else
        'do nothing
  End Select
End Sub
'====================================

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Set Target = Range("A8")
    If Target = "" Then Exit Sub
    On Error GoTo Badname
    ActiveSheet.Name = Left(Target, 31)
    Exit Sub
Badname:
    MsgBox "Please revise the entry in A8." & Chr(13) _
    & "It appears to contain one or more " & Chr(13) _
    & "illegal characters." & Chr(13)
    Range("A8").Activate
End Sub

如果要在所有工作表上运行此代码,请在双击前使用
工作簿\u工作表
事件,而不是在双击前使用
工作表

假设:
OLEObjects(“TempCombo”)
名称在所有工作表中都是相同的

    Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim str As String
    Dim cboTemp As OLEObject

    Dim ws As Worksheet
    Set ws = ThisWorkbook.ActiveSheet
        Set cboTemp = ws.OLEObjects("TempCombo")
          On Error Resume Next
          With cboTemp
          'clear and hide the combo box
            .ListFillRange = ""
            .LinkedCell = ""
            .Visible = False
          End With
        On Error GoTo errHandler
          If Target.Validation.Type = 3 Then
            'if the cell contains
              'a data validation list
            Cancel = True
            Application.EnableEvents = False
            'get the data validation formula
            str = Target.Validation.Formula1
            str = Right(str, Len(str) - 1)
            With cboTemp
              'show the combobox with the list
              .Visible = True
              .Left = Target.Left
              .Top = Target.Top
              .Width = Target.Width + 5
              .Height = Target.Height + 5
              .ListFillRange = str
              .LinkedCell = Target.Address
            End With
            cboTemp.Activate
            'open the drop down list automatically
            Me.TempCombo.DropDown
          End If

errHandler:
      Application.EnableEvents = True
      Exit Sub
    End Sub

答案可能很有趣。请在此工作簿中的每张工作表上使用
。工作表
,并在所有工作表中显示
cboTemp
。@Maddy您能否进一步阐述一下show cboTemp脚本的外观?我理解工作簿中每个工作表中的for循环,但我的循环专门调用工作表。@blairb是所有工作表中OLEObjects(“TempCombo”)的名称相同吗?当你说OLEObjects(“TempCombo”)的名称在所有工作表中都相同时,我所做的只是复制相同的工作表测试,大约5到6次,因为它是一个模板,其他用户可以填写。我假设它复制了我在初始“测试”工作表中实例化的同一个组合框名称,但当我尝试在“测试(2)”中完成一个组合框时,它会将我返回到初始工作表。有什么想法吗?