我可以重命名 excel 中每个具有相同名称的组合框吗?

Can I rename every combobox with same name in excel?

sheet中有很多组合框,它们是附加动态的。但是所有组合框的分配都是相同的。他们将 运行 宏中的一个函数。我可以重命名所有具有相同名称的组合框吗?或者我怎样才能做我想做的事?

Sub ekranadi()
  Dim mainworkBook As Workbook
  Set mainworkBook = ActiveWorkbook
  For i = 1 To mainworkBook.Sheets.Count
    If Left(mainworkBook.Sheets(i).Name, 5) = "Ekran"
     Then ComboBoxEkranAdı.AddItem mainworkBook.Sheets(i).Name
    End If
  Next i
End Sub

如果我对你的要求的理解是正确的,下面的宏将告诉你如何实现你想要的效果。

用户表单有一个名为 Controls 的集合,其中包含表单上的每个控件。如果 6 是 MyControlControls 内的索引号,您可以写 Controls(6).Name 而不是 MyControl.Name

下面的宏输出窗体上每个控件的索引号、类型名和名称。如果控件是 ComboBox,它会向其中添加三个项目,每个项目值对于该框都是唯一的。

编辑

抱歉,我没有足够仔细地阅读您的问题。我不在工作表上使用控件,因为我认为用户表单上的控件更强大、更方便。工作表上的控件由于有两种类型而变得更加复杂:从控件工具箱加载的控件和从窗体工具箱加载的控件。功能取决于您拥有的类型。

为了测试新宏 DemoWorksheet,我加载了包含两种类型控件的工作表 "Test"。该宏显示了如何通过它们的集合来填充这两种类型的组合框。

Option Explicit
Sub DemoUserForm()

  Dim InxCtrl As Long

  Load UserForm1

  With UserForm1

    For InxCtrl = 0 To .Controls.Count - 1
      Debug.Print Right(" " & InxCtrl, 2) & " " & _
                  Left(TypeName(.Controls(InxCtrl)) & Space(10), 15) & _
                  .Controls(InxCtrl).Name
      If TypeName(.Controls(InxCtrl)) = "ComboBox" Then
        With .Controls(InxCtrl)
          .AddItem InxCtrl & " A"
          .AddItem InxCtrl & " B"
          .AddItem InxCtrl & " C"
        End With

      End If

    Next

  End With

  UserForm1.Show

End Sub
Sub DemoWorksheet()

  Dim Inx As Long

  With Worksheets("Test")

    Debug.Print "Shapes.Count=" & .Shapes.Count
    Debug.Print "OLEObjects.Count=" & .OLEObjects.Count

   For Inx = 1 To .Shapes.Count
     With .Shapes(Inx)
       Debug.Print "S " & Right(" " & Inx, 2) & " ShapeType=" & _
                   ShapeTypeName(.Type) & " Name=" & .Name
       If .Type = msoFormControl Then
         Debug.Print "     FormControlType=" & FormControlTypeName(.FormControlType)
         If .FormControlType = xlDropDown Then
           .ControlFormat.AddItem "S " & Inx & " A"
           .ControlFormat.AddItem "S " & Inx & " B"
           .ControlFormat.AddItem "S " & Inx & " C"
           .ControlFormat.DropDownLines = 3
         End If
       End If
     End With
   Next
   For Inx = 1 To .OLEObjects.Count
     With .OLEObjects(Inx)
       Debug.Print "O " & Right(" " & Inx, 2) & " OleType=" & _
                   OLETypeName(.OLEType) & " Name=" & .Name
       If Left(.Name, 8) = "ComboBox" Then
         .Object.AddItem "O " & Inx & " A"
         .Object.AddItem "O " & Inx & " B"
         .Object.AddItem "O " & Inx & " C"
       End If
     End With
   Next

  End With
End Sub
Function FormControlTypeName(ByVal FCType As Long) As String

  Dim Inx As Long
  Dim TypeName() As Variant
  Dim TypeNumber() As Variant

  TypeName = Array("ButtonControl", "CheckBox", "DropDown", "EditBox", "GroupBox", _
                   "Label", "ListBox", "OptionButton", "ScrollBar", "Spinner")
  TypeNumber = Array(xlButtonControl, xlCheckBox, xlDropDown, xlEditBox, xlGroupBox, _
                     xlLabel, xlListBox, xlOptionButton, xlScrollBar, xlSpinner)

  For Inx = 0 To UBound(TypeNumber)
    If FCType = TypeNumber(Inx) Then
      FormControlTypeName = TypeName(Inx)
      Exit Function
    End If
  Next

  FormControlTypeName = "Unknown"

End Function
Function OLETypeName(ByVal OType As Long) As String

  If OType = xlOLELink Then
    OLETypeName = "Link"
  ElseIf OType = xlOLEEmbed Then
    OLETypeName = "Embed"
  ElseIf OType = xlOLEControl Then
    OLETypeName = "Control"
  Else
    OLETypeName = "Unknown"
  End If

End Function
Function ShapeTypeName(ByVal SType As Long) As String

  Dim Inx As Long
  Dim TypeName() As Variant
  Dim TypeNumber() As Variant

  TypeName = Array("AutoShape", "Callout", "Canvas", "Chart", "Comment", "Diagram", _
                   "EmbeddedOLEObject", "FormControl", "Freeform", "Group", "Line", _
                   "LinkedOLEObject", "LinkedPicture", "Media", "OLEControlObject", _
                   "Picture", "Placeholder", "ScriptAnchor", "ShapeTypeMixed", _
                   "Table", "TextBox", "TextEffect")
  TypeNumber = Array(msoAutoShape, msoCallout, msoCanvas, msoChart, msoComment, msoDiagram, _
                   msoEmbeddedOLEObject, msoFormControl, msoFreeform, msoGroup, msoLine, _
                   msoLinkedOLEObject, msoLinkedPicture, msoMedia, msoOLEControlObject, _
                   msoPicture, msoPlaceholder, msoScriptAnchor, msoShapeTypeMixed, _
                   msoTable, msoTextBox, msoTextEffect)


  For Inx = 0 To UBound(TypeNumber)
    If SType = TypeNumber(Inx) Then
      ShapeTypeName = TypeName(Inx)
      Exit Function
    End If
  Next

  ShapeTypeName = "Unknown"

End Function