vba 清除用户窗体上多页控件中的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16225079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:50:50  来源:igfitidea点击:

Clear Text boxes within a Multipage Control on Userform

excelvbaexcel-vba

提问by Bradley Carrico

I am working on a school project while learning vba for the first time. The project is built around a wedding planning worksheet. I plan to lockdown the worksheet so that formulas and formatting don't get broken by the user (my fiance would find a way). In order to do this, I am building a userform that collects the data via text boxes on different pages of a multipage control.

我在第一次学习 vba 时正在做一个学校项目。该项目是围绕婚礼策划工作表构建的。我计划锁定工作表,以便用户不会破坏公式和格式(我的未婚夫会找到方法)。为了做到这一点,我正在构建一个用户窗体,它通过多页控件不同页面上的文本框收集数据。

This example only has one page, but it will show others like "Apparel" later: enter image description here

此示例只有一页,但稍后会显示其他页面,例如“Apparel”: 在此处输入图片说明

I currently have the following code on _Click for the "Clear" button.

我目前在 _Click 上有以下代码用于“清除”按钮。

Dim C As Control


For Each C In frmExpenses.Controls
    If TypeName(C) = "TextBox" Then
        C.Value = ""
    End If
Next C

This effectively clears the data from the text boxes. However, I want to be able to clear the data on ONLY the active page on the multipage. For example if there is later a tab called "Flowers" and it's the active tab, I want to leave the data on "Apparel" and clear the data from "Flowers."

这有效地清除了文本框中的数据。但是,我希望能够仅清除多页活动页面上的数据。例如,如果稍后有一个名为“Flowers”的选项卡并且它是活动选项卡,我想将数据保留在“Apparel”上并清除“Flowers”中的数据。

I tried being as thorough as possible above. Let me know, if you need clarification on something.

我试着在上面尽可能彻底。如果您需要澄清某些事情,请告诉我。

回答by Siddharth Rout

Try this

尝试这个

For a particular Page (Say 1)

对于特定页面(例如 1)

Dim ctl As Control

For Each ctl In Me.MultiPage1.Pages(1).Controls
    If TypeName(ctl) = "TextBox" Then  ctl.Value = ""
Next

From ActivePage

从 ActivePage

Dim ctl As Control

For Each ctl In Me.MultiPage1.Pages(Me.MultiPage1.Value).Controls
    If TypeName(ctl) = "TextBox" Then ctl.Value = ""
Next