我不确定你是怎么称呼它的,但这里有一个独立的过程,应该会有所帮助:
Sub test()
Dim ws As Excel.Worksheet
Dim ProductCombo As OLEObject
Dim ProductText As OLEObject
Set ws = ThisWorkbook.Sheets(1)
With ws
Set ProductCombo = .OLEObjects("Product1")
Set ProductText = .OLEObjects(ProductCombo.Name & "_status")
ProductText.Enabled = ProductCombo.Object.Text <> "Disabled"
End With
End Sub
编辑:我真的很讨厌工作表控件 - 每次编程时我都从头开始!尽管如此,我想我会添加这个子例程,它会根据其配对组合框重置名称符合模式 Product#_status 的每个文本框。逻辑确实假设名称以 Product1、Product2 等开头,编号中没有间隙:
Sub test2()
Dim ws As Excel.Worksheet
Dim ctl As OLEObject
Dim i As Long
Dim ProductComboboxesCount
Dim ProductCombo As OLEObject
Dim ProductText As OLEObject
Const ControlPrefix As String = "Product"
Set ws = ThisWorkbook.Sheets(1)
With ws
For Each ctl In .OLEObjects
If TypeOf ctl.Object Is MSForms.ComboBox And Left(ctl.Name, Len(ControlPrefix)) = ControlPrefix Then
ProductComboboxesCount = ProductComboboxesCount + 1
End If
Next ctl
For i = 1 To ProductComboboxesCount
Set ProductCombo = .OLEObjects(ControlPrefix & i)
Set ProductText = .OLEObjects(ControlPrefix & i & "_status")
ProductText.Enabled = ProductCombo.Object.Text <> "Disabled"
Next i
End With
End Sub