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

时间:2015-02-27 09:10:26

标签: excel-vba combobox vba excel

表格中有很多组合框,它们都是动态的。但所有组合框都是'作业是一样的。他们将在宏中运行一个函数。我可以重命名所有具有相同名称的组合框吗?或者我怎么能做我想做的事?

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

1 个答案:

答案 0 :(得分:1)

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

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

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

修改

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

为了测试新的宏DemoWorksheet,我加载了工作表"测试"两种控制方式。该宏显示了如何通过其集合填充这两种类型的组合框。

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