简体   繁体   English

如何在一个单元格中添加多个值

[英]How to add multiple values into one single cell

Private Sub CommandButton1_Click()
 Dim ctrl As control
   For Each ctrl In UserForm1.Controls
     If TypeName(ctrl) = "CheckBox" Then
       'Pass this CheckBox to the subroutine below:
     TransferValues ctrl
     End If
   Next
End Sub

Sub TransferValues(cb As MSForms.CheckBox)
 Dim ws As Worksheet
 Dim emptyRow As Long
 Dim ws1 As Worksheet

If cb Then
   'Define the worksheet based on the CheckBox.Name property:
    Set ws = Sheets(Left(cb.Name, 15))
    emptyRow = WorksheetFunction.CountA(ws.range("A:A")) + 1
       With ws
           .Cells(emptyRow, 1).Value = surname.Value
           .Cells(emptyRow, 2).Value = firstname.Value
           .Cells(emptyRow, 3).Value = tod.Value
           .Cells(emptyRow, 4).Value = program.Value
           .Cells(emptyRow, 5).Value = email.Value
           .Cells(emptyRow, 6).Value = officenumber.Value
           .Cells(emptyRow, 7).Value = cellnumber.Value
        End With
    Set ws1 = Sheets("Master")
    emptyRow = WorksheetFunction.CountA(range("A:A")) + 1
        With ws1
            .Cells(emptyRow, 1).Value = surname.Value
            .Cells(emptyRow, 2).Value = firstname.Value
            .Cells(emptyRow, 3).Value = tod.Value
            .Cells(emptyRow, 4).Value = program.Value
            .Cells(emptyRow, 5).Value = email.Value
            .Cells(emptyRow, 6).Value = officenumber.Value
            .Cells(emptyRow, 7).Value = cellnumber.Value
            .Cells(emptyRow, 8).Value = cb.Name
        End With
  End If
'the master sheet needs to have a "Stakeholder" column with list of stakeholder the person belongs to

The problem here is Cb.Name - I want the name checkboxes to appear in one single cell but right now it's making extra rows depending on the number of checked boxes. 这里的问题是Cb.Name-我希望名称复选框出现在一个单元格中,但是现在,它根据复选框的数量增加了额外的行。 So instead of putting 6/8 checked boxes names into 1 single cell it makes 6 rows with each names which is not good. 因此,与其将6/8复选框的名称放入单个单元格中,不如将每个名称分成6行,这是不好的。 How do i transfer all cb.names into one single cell? 如何将所有cb.name转移到一个单元格中?

sorry if the code doesn't look properly formatted - for some reason it's not showing all the indents... 很抱歉,如果代码的格式不正确-由于某种原因,它没有显示所有缩进...

If I read you right your transfer function deals with individual checks but the master sheet needs to have a single line for the whole lot, right? 如果我没看错,您的转移函数处理的是单笔支票,但母版表需要整行都用一行,对吗?

If so what you will need to do is work on the individual and collective levels separately. 如果是这样,您需要做的是分别在个人和集体层面上进行工作。 Delete all references to the master sheet from your basic sub and deal with the master sheet on it's own 从您的基本子目录中删除对主表的所有引用,然后自行处理该主表

Sub TransferMasterValue()
Dim allChecks As String

'Iterate through the checkboxes concatenating a string of all names
For Each ctrl In UserForm1.Controls
    If TypeName(ctrl) = "CheckBox" Then
        If ctrl Then
            allChecks = allChecks & ctrl.Name & ","
        End If
    End If
Next

'If you have at least one transfer to the Master sheet
If Len(allChecks) > 0 Then
    'Your code to transfer
    Set ws1 = Sheets("Master")
    emptyRow = WorksheetFunction.CountA(range("A:A")) + 1

    With ws1
        .Cells(emptyRow, 1).Value = surname.Value
        ...
        'and post the concatenated value in the name position
        .Cells(emptyRow, 8).Value = left(allChecks,len(allChecks)-1)
    End With
End If
End Sub

The main button click function will then look like... 主按钮单击功能将看起来像...

Private Sub CommandButton1_Click()
 Dim ctrl As control
   For Each ctrl In UserForm1.Controls
     If TypeName(ctrl) = "CheckBox" Then
       'Pass this CheckBox to the subroutine below:
     TransferValues ctrl
     End If
   Next

   TransferMasterValue
End Sub

Have you tried concatenate? 您是否尝试过串联? If I have cells |1|2|3|4|5| 如果我有单元格| 1 | 2 | 3 | 4 | 5 | I can just insert the function =CONCATENATE(A1,",",B1) and you can change to "," to " " for a space, or have no separation at all. 我只需要插入= CONCATENATE(A1,“,”,B1)函数,就可以将“,”更改为“”,或者完全不分隔。 If this isn't what you're looking for then I think I misinterpted the question. 如果这不是您要找的东西,那么我想我误解了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM