VBA多个复选框userform到单个单元格

时间:2015-01-06 03:22:09

标签: excel vba userform

我正在使用带有多个复选框的userform。我的目标是让所有复选框将其值(唯一的两个字母的首字母缩略词)返回到单个单元格。我不知道如何做到这一点。我今天没有VBA经验。

3 个答案:

答案 0 :(得分:0)

您可以录制宏来获取VBA的其他相关代码,但这应该会有所帮助:

Range("A" & LastRow).Value = CheckBox1.Value & CheckBox2.Value & CheckBox3.Value 'etc.
  'Concatenate CheckBox values with "&" (the values will be True or False though)

答案 1 :(得分:0)

将每个复选框的linkcell设置为不同的单元格 - 例如。 checkbox1到单元格A1,checkbox2到单元格A2等 对于每个链接单元格,在其相邻单元格中使用If公式来测试链接单元格的内容并返回字符串或空白, 例如如果(A1 = True," AA","")在单元格B1中,if(A2 = True," BB","" ;)在单元格B2等中 最后,连接' if'另一个细胞中的配方:例如B1&","& B2&","& B3,使用逗号作为分隔符

答案 2 :(得分:0)

为此,您可以在表单中创建所有复选框的集合。继承我的代码:

Private mcolCHK As Collection 'This creates a modular level collection, which will keep track of all of the checkboxes
Private Sub cmdOK_Click()

    Dim objA As Object, strResults As String, i As Integer
    i = 1
    For Each objA In mcolCHK
        strResults = strResults & i & Left(objA.Value, 1)
        i = 1 + i
    Next
    Unload frmTestForm 'rename this the name of your form

    MsgBox strResults
    'instead of using a message box, you can say "strResults = Sheets(sheet1).range("A1").value
    'This part may be customized as needed
End Sub

Private Sub UserForm_Initialize()
    Set mcolCHK = New Collection
    mcolCHK.Add chkAtt1  'you will have to add one of these for each checkbox (simply replace "chkAtt1" with the name of your first checkbox
    mcolCHK.Add chkAtt2   'and repeat for each checkbox
End Sub

复选框始终给出True或False值。使用此方法,您将获得复选框的编号(按照如何将它们添加到UserForm_Initialize()子集中的集合的顺序)和结果的第一个字母(T或F)。当我运行它时,我有两个复选框,并在表单运行时检查每个复选框。这给了我“1T2T”的结果。这可以根据需要进行更改。你真正需要的就是这一切的运作方式。只需确保更改所有名称以匹配您使用的变量的名称。