如何通过名称获得控制名称? [英] How to get a name of control by name?

查看:111
本文介绍了如何通过名称获得控制名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的功能,那里有一个组合框。如果组合框的值等于禁用,我将禁用文本框B.有很多组合框及其对应的文本框B,排列成行并用手命名。如果组合框A被命名为 Product1 ,则文本框B将被命名为 Product1_status



我正在想的是:

 如果value_of_a =disable然后
Dim name_of_b as String
name_of_b = Me.Combo.Name +_status
get_object_by_name(name_of_b).Enabled = False
如果

我该怎么做?

解决方案

我不知道你怎么样正在调用这个,但这是一个独立的程序,应该有助于:

 子测试()

Dim ws As Excel.Worksheet
Dim ProductCombo As OLEObject
Dim ProductText As OLEObject

设置ws = ThisWorkbook.Sheets(1)
带有ws
设置ProductCombo = .OLEObjects(Product1)
设置ProductText = .OLEObjects(ProductCombo.Name&_status)
ProductText.Enabled = ProductCombo.Object.Text< 禁用
结束
结束Sub

编辑:我真的很讨厌工作表控制 - 我每次编程时都从头开始!尽管如此,我想我会添加这个子程序,根据其配对的组合框,重置每个文本框的名称符合图案产品#_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作为OLEObject
Const ControlPrefix As String =Product

设置ws = ThisWorkbook.Sheets(1)
带有ws
对于每个ctl在.OLEObjects
如果TypeOf ctl.Object是MSForms.ComboBox和Left(ctl.Name,Len(ControlPrefix))= ControlPrefix Then
ProductComboboxesCount = ProductComboboxesCount + 1
End If
Next ctl
对于i = 1 To ProductComboboxesCount
设置ProductCombo = .OLEObjects(ControlPrefix& i)
设置ProductText = .OLEObjects(ControlPrefix& i&_status)
ProductText.Enabled = ProductCombo .Object.Text<> 残疾人
下一个i
结束
结束子


I have a simple function where there's a combo box. If combo box's value is equal to "Disable", I'll disable textbox B. There are many combo boxes with their corresponding textbox B, arranged in rows and named by hand. If combo box A is named Product1, textbox B will be named Product1_status

I was thinking something like:

If value_of_a = "disable" Then 
 Dim name_of_b as String
 name_of_b = Me.Combo.Name + "_status"
 get_object_by_name(name_of_b).Enabled = False
End If

How do I do this?

解决方案

I'm not sure how you are calling this, but here's a self-contained procedure that should help:

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

EDIT: I really hate worksheet controls - I start from scratch every time I program them! Nonetheless, I thought I'd add this subroutine which resets every textbox whose name fits the patter Product#_status, according to its paired combobox. The logic does assume the names start with Product1, Product2, etc., without a gap in the numbering:

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

这篇关于如何通过名称获得控制名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆