2014-01-28 39 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    
    End If 

    'the master sheet needs to have a "Stakeholder" column with list of stakeholder the person belongs to 
End Sub 

根据哪个复选框被选中,我想将复选框的值编译到Master Tab和Master Tab中的一个单元格中。以上代码根据个人所属利益相关者传输每个文本框的值(并通过复选框完成)如何将信息从vba添加到多个工作表

例如,名为John Doe的人属于6/8复选框,上面的代码将所有信息传输到6/8个复选框表单。但我总是希望在主选项卡中填充信息,并添加一个列名干系人,它将传输选中框的名称。当我尝试它时,它为每个复选框创建了一个单独的行,而不是将它编译为一个单元格。所以我做了6个John Doe's,除了每个John Doe都有不同的利益相关者他都属于这个。

回答

0

我们将使用VBA完成此操作,下面的过程说明了如何完成此操作。

子copyPasteData()昏暗strSourceSheet作为字符串昏暗strDestinationSheet作为字符串昏暗LASTROW只要

strSourceSheet = “数据输入”

表(strSourceSheet)。可见=真表(strSourceSheet)。选择

Range(“C2”)。Select Active While ActiveCell.Value <>“”strDestinationSheet = ActiveCell.Value ActiveCell.Offset(0,-2).Resize(1,ActiveCell.CurrentRegion.Columns.Count).Select Selection 。复制表格(strDestinationSheet).Visible = True表格(strDestinationShe选择lastRow = LastRowInOneColumn(“A”)Cells(lastRow + 1,1).Select Selection.PasteSpecial xlPasteValues Application.CutCopyMode = False Sheets(strSourceSheet).Select ActiveCell.Offset(0,2).Select ActiveCell.Offset (1,0)。选择循环结束Sub

+1

请正确格式化您的代码(选择并按ctrl-k按4格缩进并触发格式化)。 – o11c

相关问题