代码之家  ›  专栏  ›  技术社区  ›  youpilat13 Ty Petrice

设置标题列的名称-应为14个列名称

  •  1
  • youpilat13 Ty Petrice  · 技术社区  · 7 年前

    我有一个简单的用户表单,它收集有关人员的信息。

    这里有一个例子,我在添加6个人的信息时得到了什么。我想从第3行开始列出信息(因为前2行由命令按钮“remplir formulaire”填充):

    Example of filled form

    我的问题是,在调用userform时,我希望每个前14列有14个头名称(将值填充到这些列中的函数将在稍后的代码中完成)。

    要设置14个字段的名称(从第3行开始),我执行了以下操作:

    Private Sub ResetForm()
    
    'Monsieur by default
    ComboBox1.Value = "Monsieur"
    
    'Empty TextBox1
    TextBox1.Value = ""
    
    'Empty TextBox2
    TextBox2.Value = ""
    
    'Empty TextBox3
    TextBox3.Value = ""
    
    End Sub
    
    Private Sub UserForm_Initialize()
    
    'Create header for each colum
    Dim HeaderName(14) As String
    
    'Index to browse HeaderName array
    Dim a As Integer
    
    HeaderName(1) = "Civilité"
    HeaderName(2) = "Nom"
    HeaderName(3) = "Prénom"
    HeaderName(4) = "Âge"
    HeaderName(5) = "Fonction"
    HeaderName(6) = "Entité"
    HeaderName(7) = "Catégorie"
    HeaderName(8) = "Adresse"
    HeaderName(9) = "Code postal"
    HeaderName(10) = "Ville"
    HeaderName(11) = "Tél Fixe"
    HeaderName(12) = "Tél Portable"
    HeaderName(13) = "Email"
    HeaderName(14) = "Autres infos"
    
    'Initlialize headers : start from row = 3
    Sheet1.Activate
    With Sheet1
          For a = 1 To 14
               If (.Cells(3, a) <> "") Then
                 .Cells(3, a).Value = HeaderName(a)
               End If
               Debug.Print "a = " & a
          Next a
    End With
    
    'Fill ComboBox
    With ComboBox1
        .AddItem "Monsieur"
        .AddItem "Madame"
    End With
    
    'Set Elu by default
    CheckBox1.Value = True
    CheckBox2.Value = False
    
    'Reset all inputs
    Call ResetForm
    
    End Sub
    

    在同一个VBA源中,我对命令按钮“remplir formulaire”执行了以下操作:

    Private Sub CommandButton1_Click()
    
    Dim emptyRow As Long
    
    Sheet1.Activate
    
    With Sheet1
    emptyRow = .UsedRange.Rows.Count + .UsedRange.Rows(1).Row - 1
        If .Cells(emptyRow, 1).Value <> "" Then
            emptyRow = emptyRow + 1
            .Cells(emptyRow, 1).Value = ComboBox1.Value
            .Cells(emptyRow, 2).Value = TextBox1.Value
            .Cells(emptyRow, 3).Value = TextBox2.Value
            .Cells(emptyRow, 4).Value = TextBox3.Value
            .Cells(emptyRow, 5).Value = CheckBox1.Value
    
            If CheckBox1.Value = True Then
               .Cells(emptyRow, 5).Value = CheckBox1.Caption
            Else
               .Cells(emptyRow, 5).Value = CheckBox2.Caption
            End If
        End If
    End With
    
    End Sub
    

    所以在“不明白为什么我不能得到函数中指定的每一列的14个名称” UserForm_Initialize() 通过 HeaderName 数组:当我单击“remplir formulaire”命令按钮时,只显示前四个(文明)、nom、pr nom、age),而不是其他10个命令。

    有人知道怎么了吗?当做

    PS:.XLSM不可从下载 this link

    1 回复  |  直到 7 年前
        1
  •  1
  •   user9298223    7 年前

    你真的需要检查一下手机是否是空的吗?我将用以下代码替换20多行代码:

    Sheet1.Range("A3").Resize(1, 14).Value = Array("Civilité", "Nom", "Prénom", "Âge", "Fonction", "Entité", "Catégorie", "Adresse", "Code postal", "Ville", "Tél Fixe", "Tél Portable", "Email", "Autres infos")