سؤال

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. So instead of putting 6/8 checked boxes names into 1 single cell it makes 6 rows with each names which is not good. How do i transfer all cb.names into one single cell?

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| I can just insert the function =CONCATENATE(A1,",",B1) and you can change to "," to " " for a space, or have no separation at all. If this isn't what you're looking for then I think I misinterpted the question.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top