简体   繁体   English

VBA Excel - 定位用户表单控件

[英]VBA Excel - Targeting userform controls

I'm trying to make code that, depending on an integer (which loops from 0 to 4), changes the targeted userform label.我正在尝试根据一个整数(从 0 到 4 循环)更改目标用户表单标签的代码。 I've tried using我试过使用

Eg例如

Public placeholderLabel as label 

sub test()
    if loopInt = 1

         set placeholderLabel = userForm1.labelTestA

    elseif loopInt = 2 then

       set placeholderLabel = userForm1.LabelTestB

    End if 
    placeholderLabel.caption "Woohoo!")
end sub 

I've tried userform1.controls.LabelTestA and this did not do anything.我试过 userform1.controls.LabelTestA 但这没有做任何事情。

My intention was to be able to loop from 0 to 4 and then depending on the loopInt change 1 of the 5 labels.我的目的是能够从 0 循环到 4,然后根据 loopInt 更改 5 个标签中的 1 个。 At this point it would have been way easier for me to just repeat the 5 assignments instead of looping 0 to 4 and changing the placeholders...but I want to understand why it is that my assignments to these userform objects isn't working.在这一点上,我只需重复 5 个分配而不是循环 0 到 4 并更改占位符会更容易......但我想了解为什么我对这些用户窗体对象的分配不起作用。 Very frustrating!非常令人沮丧!

placeholderLabel.caption "Woohoo!") is improper. placeholderLabel.caption "Woohoo!")是不正确的。 try placeholderLabel.caption = "Woohoo!"试试placeholderLabel.caption = "Woohoo!"

If the name of your control is "myControl" then UserForm1.Controls("myControl") will get you the object如果您的控件名称是“myControl”,则UserForm1.Controls("myControl")将为您提供对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM