【问题标题】:Loop Label names in userform picking text from worksheet range用户窗体中的循环标签名称从工作表范围中选择文本
【发布时间】:2016-07-21 06:25:18
【问题描述】:

我在用户表单中有 27 个标签(例如标签 500 到 526),每个标签都必须从一个范围(sheet1-范围 b4 到 b30)中选择名称。如果标签名称为空,则应隐藏相应的文本框(文本框 500 到 526)。在循环此代码时需要帮助。

proviate sub teamlistchange()

    Label500.Caption = Worksheets("SME - Data classification").Range("b4").Value
Label501.Caption = Worksheets("SME - Data classification").Range("b5").Value
Label502.Caption = Worksheets("SME - Data classification").Range("b6").Value
Label503.Caption = Worksheets("SME - Data classification").Range("b7").Value
Label504.Caption = Worksheets("SME - Data classification").Range("b8").Value
Label505.Caption = Worksheets("SME - Data classification").Range("b9").Value
Label506.Caption = Worksheets("SME - Data classification").Range("b10").Value
Label507.Caption = Worksheets("SME - Data classification").Range("b11").Value
Label508.Caption = Worksheets("SME - Data classification").Range("b12").Value
Label509.Caption = Worksheets("SME - Data classification").Range("b13").Value
Label510.Caption = Worksheets("SME - Data classification").Range("b14").Value
Label511.Caption = Worksheets("SME - Data classification").Range("b15").Value
Label512.Caption = Worksheets("SME - Data classification").Range("b16").Value
Label513.Caption = Worksheets("SME - Data classification").Range("b17").Value
Label514.Caption = Worksheets("SME - Data classification").Range("b18").Value
Label515.Caption = Worksheets("SME - Data classification").Range("b19").Value
Label516.Caption = Worksheets("SME - Data classification").Range("b20").Value
Label517.Caption = Worksheets("SME - Data classification").Range("b21").Value
Label518.Caption = Worksheets("SME - Data classification").Range("b22").Value
Label519.Caption = Worksheets("SME - Data classification").Range("b23").Value
Label520.Caption = Worksheets("SME - Data classification").Range("b24").Value
Label521.Caption = Worksheets("SME - Data classification").Range("b25").Value
Label522.Caption = Worksheets("SME - Data classification").Range("b26").Value
Label523.Caption = Worksheets("SME - Data classification").Range("b27").Value
Label524.Caption = Worksheets("SME - Data classification").Range("b28").Value
Label525.Caption = Worksheets("SME - Data classification").Range("b29").Value
Label526.Caption = Worksheets("SME - Data classification").Range("b30").Value

'SDM activities hide empty cells
If Label500.Caption = "" Then TextBox500.Visible = False
If Label501.Caption = "" Then TextBox501.Visible = False
If Label502.Caption = "" Then TextBox502.Visible = False
If Label503.Caption = "" Then TextBox503.Visible = False
If Label504.Caption = "" Then TextBox504.Visible = False
If Label505.Caption = "" Then TextBox505.Visible = False
If Label506.Caption = "" Then TextBox506.Visible = False
If Label507.Caption = "" Then TextBox507.Visible = False
If Label508.Caption = "" Then TextBox508.Visible = False
If Label509.Caption = "" Then TextBox509.Visible = False
If Label510.Caption = "" Then TextBox510.Visible = False
If Label511.Caption = "" Then TextBox511.Visible = False
If Label512.Caption = "" Then TextBox512.Visible = False
If Label513.Caption = "" Then TextBox513.Visible = False
If Label514.Caption = "" Then TextBox514.Visible = False
If Label515.Caption = "" Then TextBox515.Visible = False
If Label516.Caption = "" Then TextBox516.Visible = False
If Label517.Caption = "" Then TextBox517.Visible = False
If Label518.Caption = "" Then TextBox518.Visible = False
If Label519.Caption = "" Then TextBox519.Visible = False
If Label520.Caption = "" Then TextBox520.Visible = False
If Label521.Caption = "" Then TextBox521.Visible = False
If Label522.Caption = "" Then TextBox522.Visible = False
If Label523.Caption = "" Then TextBox523.Visible = False
If Label524.Caption = "" Then TextBox524.Visible = False
If Label525.Caption = "" Then TextBox525.Visible = False
If Label526.Caption = "" Then TextBox526.Visible = False

end sub

【问题讨论】:

    标签: vba for-loop label userform caption


    【解决方案1】:

    试试这样的:

    proviate Sub teamlistchange()
    
    Dim i As Integer
    
    For i = 500 To 526
    
    Controls("Label" & i).Caption = Worksheets("SME - Data classification").Range(Cells(i,2)).Value
    If Controls("Label" & i).Caption = "" Then Controls("TextBox" & i).Visible = False
    
    Next i
    

    希望这会有所帮助!

    【讨论】:

    • 感谢您的快速回复,但是,我收到“控件(“标签”和 i)的错误。Caption = Worksheets(“SME - 数据分类”).Range(单元格(i, 2)).值”
    • "Worksheets("SME - 数据分类").Range(Cells(i,2)).Value" 这里的范围是b4到b30。
    • 找到解决方案的运气好吗??
    • 你得到什么样的错误?
    • 它说应用程序定义或对象定义错误。我认为在定义范围时,我们给出了工作表(“SME - 数据分类”).range(celld(i,2)).value。这里我从 500 开始。但是,我的愤怒是“b4:b30”。你认为这是错误吗?
    【解决方案2】:

    解决了!!

    Private Sub TeamListChange()
    
    Const iCONTROL_NO_FIRST As Integer = 500
    Const iCONTROL_NO_LAST  As Integer = 526
    Const sPREFIX_TEXTBOX   As String = "TextBox"
    Const sPREFIX_LABEL     As String = "Label"
    Const sLABEL_COLUMN     As String = "B"
    Const sSHEET_NAME       As String = "SME - Data classification"
    Const iOFFSET           As Integer = 496
    
    Dim iLabelRowNo         As Integer
    Dim sLabelText          As String
    Dim rLabelCell          As Range
    Dim iControlNo          As Integer
    Dim wks                 As Worksheet
    
    Set wks = ThisWorkbook.Worksheets(sSHEET_NAME)
    
    With Me
    
        For iControlNo = iCONTROL_NO_FIRST To iCONTROL_NO_LAST
    
            iLabelRowNo = iControlNo - iOFFSET
    
            Set rLabelCell = wks.Range(sLABEL_COLUMN & CStr(iLabelRowNo))
    
            sLabelText = rLabelCell.Value
    
            .Controls(sPREFIX_LABEL & CStr(iControlNo)).Caption = sLabelText
    
            If sLabelText = vbNullString Then
                .Controls(sPREFIX_TEXTBOX & CStr(iControlNo)).Visible = False
            End If
    
        Next iControlNo
    
    End With
    

    结束子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 2023-03-21
      相关资源
      最近更新 更多