勾选复选框后,标题将被放入以逗号分隔的数组中

问题描述 投票:0回答:1

我想创建一个代码,其中当选中复选框时,其标题将被放入一个数组中并用逗号分隔。对于复选框 1 到 12,它将用于利润列;对于复选框 13 到 24,它将用于资本列。这是我想插入复选框代码的代码。

Private Sub SUBMIT_Click()

    Dim sh As Worksheet, msg As String, arr, c As Range, id
    
    'check for any empty required fields
    If Len(TextBox1.Value) = 0 Then msg = msg & vbLf & " - Employee ID"

    If Len(msg) > 0 Then 'anything missing?
        MsgBox "The following fields are required:" & msg, _
                vbOKOnly + vbCritical, "Missing Information"
    Else
        'OK to write to sheet
        Set sh = ThisWorkbook.Sheets("Bulk Loader File")
        Set c = sh.Cells(Rows.Count, "A").End(xlUp).Offset(1) '##start adding here
        arr = Split(TextBox1.Value, ",") '##split on comma to get an array
        
            For i = 21 To 35 Step 2                           '## loop over comboboxes
            dataItem = Me.Controls("ComboBox" & i).Value '## read combo values...
            schedule = Me.Controls("ComboBox" & (i + 1)).Value
            If Len(dataItem) > 0 Then             '## any value selected?
            
        For Each id In arr               '##loop over the array
             c.Resize(1, 30).Value = Array(Trim(id), TextBox2.Value, TextBox3.Value, TextBox4.Value, TextBox5.Value, TextBox6.Value, TextBox7.Value, TextBox8.Value, _
                                     ComboBox7.Value, ComboBox8.Value, ComboBox9.Value, ComboBox10.Value, ComboBox11.Value, _
                                     ComboBox12.Value, ComboBox13.Value, ComboBox14.Value, ComboBox15.Value, ComboBox16.Value, _
                                     ComboBox17.Value, ComboBox1.Value, ComboBox2.Value, ComboBox3.Value, ComboBox4.Value, _
                                     ComboBox5.Value, ComboBox6.Value, ComboBox18.Value, ComboBox19.Value, ComboBox20.Value, dataItem, schedule, Profits, Capital)
             
            Set c = c.Offset(1)          '##next output row
                Next id
            End If
        Next i

        MsgBox "Information Loaded", vbOKOnly + vbInformation, "Notification"
        Unload Me
    End If
    
End Sub

这是我的复选框代码草案,但我不确定这是否正确以及如何将其放置在我当前的代码中。

私有子CommandButton1_Click()

Dim sh As Worksheet, msg As String, arr, c As Range, id

'check for any empty required fields
If Len(TextBox1.Value) = 0 Then msg = msg & vbLf & " - Employee ID"

If Len(msg) > 0 Then 'anything missing?
    MsgBox "The following fields are required:" & msg, _
            vbOKOnly + vbCritical, "Missing Information"
Else
    'OK to write to sheet
    Set sh = ThisWorkbook.Sheets("Bulk Loader File")
    Set c = sh.Cells(Rows.Count, "A").End(xlUp).Offset(1) '##start adding here
    arr = Split(CheckBox1.Caption, ",")
    
    For Each xcontrol In Me.Controls

    If TypeName(xcontrol) = "CheckBox" And xcontrol.Value = True Then
    
    CheckBox1.Value = "Jan"
    CheckBox2.Value = "Feb"
    CheckBox3.Value = "Mar"
    CheckBox4.Value = "Apr"
    CheckBox5.Value = "May"
    CheckBox6.Value = "Jun"
    CheckBox7.Value = "Jul"
    CheckBox8.Value = "Aug"
    CheckBox9.Value = "Sep"
    CheckBox10.Value = "Oct"
    CheckBox11.Value = "Nov"
    CheckBox12.Value = "Dec"
    
    
    For i = 1 To 12 Step 2                           '## loop over comboboxes
    Profits = Me.Controls("Checkbox" & i).Value '## read combo values...
    If Len(Dividend) > 0 Then             '## any value selected?

    For Each id In arr               '##loop over the array
        c.Resize(1, 2).Value = Array(Trim(id), Dividend)
        Set c = c.Offset(1)          '##next output row
            Next id
        End If
    Next i

    MsgBox "Time Series Attributes Loaded", vbOKOnly + vbInformation, "Notification"
    Unload Me
End If

结束子

请参阅下面的示例用户表单和我想要执行的操作。当用户勾选其中一个框时,其标题将被放入逗号分隔的字符串中。复选框 1-12 将用于表格第 31 列中的利润列,复选框 13-24 将用于资本。

enter image description here enter image description here

excel vba
1个回答
0
投票

您只需要一个 For 循环即可收集复选框值。

仅相关部分:

Private Sub CommandButton1_Click()

    Dim sh As Worksheet, msg As String, arr, c As Range, id
    Dim profits As String, capitals As String, i As Long
    
    'check for any empty required fields
    If Len(TextBox1.Value) = 0 Then msg = msg & vbLf & " - Employee ID"
    
    If Len(msg) > 0 Then 'anything missing?
        MsgBox "The following fields are required:" & msg, _
                vbOKOnly + vbCritical, "Missing Information"
        Exit Sub
    End If
    
    'OK to write to sheet
    Set sh = ThisWorkbook.Sheets("Bulk Loader File")
    Set c = sh.Cells(Rows.Count, "A").End(xlUp).Offset(1)
    'arr = Split(CheckBox1.Caption, ",") 'what does this do ????
    
    For i = 1 To 12
        'if checkboxes are checked then add month name to appropriate string
        If Me.Controls("Checkbox" & i).Value Then
            AddWithComma profits, MonthName(i)
        End If
        If Me.Controls("Checkbox" & (12 + i)).Value Then
            AddWithComma capitals, MonthName(i)
        End If
    Next i
        
    Debug.Print "Profits: " & profits
    Debug.Print "Capitals: " & capitals
    
End Sub

'Return month name for month# `monthNum`
Function MonthName(monthNum As Long) As String
    MonthName = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", _
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")(monthNum - 1) 'zero-based array
End Function

'utility sub for adding to a string as a comma-separated value
Sub AddWithComma(ByRef msg As String, addThis As String)
    msg = msg & IIf(Len(msg) > 0, ",", "") & addThis
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.