2014-01-29 48 views
0
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 

是Cb.Name - 我想要的名字複選框出現在一個單細胞,但現在它使取決於檢查箱子的數量額外行。因此,不是將6/8個複選框名稱放入1個單元格中,而是使用6行,每個名稱都不好。我如何將所有cb.names轉移到單個單元格中?這裏的問題如何將多個值添加到一個單細胞

對不起,如果代碼看起來不正確格式化 - 由於某種原因,它沒有顯示所有縮進...

回答

1

如果我讀了你的權利,你的傳遞函數處理單獨的檢查,但主表單需要有一個單一的線,對嗎?

如果是這樣,你將需要做的是分別在個人和集體層面的工作。從你的基本分上刪除對主表中所有引用,並處理好它自己

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 

然後主按鈕的點擊功能看起來像......

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 
+0

嗨!所以我已經添加了代碼(並從它的外觀看起來就是我所需要的),但是當我因爲某種原因將其放入CommandButton1不再工作?我嘗試添加一個人但沒有任何反應..? – Doolie1106

+0

所以其他工作表的工作,但沒有出現在主標籤,我似乎無法弄清楚爲什麼......? – Doolie1106

+1

只是爲了保持代碼不變,我遺漏了原來的按鈕點擊。你必須做一個很小的調整 –

0

你試過連擊?如果我有單元格| 1 | 2 | 3 | 4 | 5 |我可以插入函數= CONCATENATE(A1,「,」,B1),並且可以將空格變爲「,」變爲「」,或者根本不分離。如果這不是你想要的,那麼我想我誤解了這個問題。

+0

不知道原材我會怎樣連接Cb.name ...本質上我想所有選中的框的名稱出現在.Cells(emptyRow,8) - 有點像包裝文本。對於eaxmple,我有複選框名爲:A,B,C,D,當我點擊CommandButton1時,我想要名稱A,B,C,D出現在一個單元格中,但現在它正在製作4個額外的行A,B,C, D.那有意義嗎..? – Doolie1106

相關問題