如何将VBA的信息添加到多个床单


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

取决于哪些复选框的chek,我想仅在主选项卡和主选项卡中将复选框的值编译到一个单元格中。上面代码转移每个文本框的值,根据该人属于的利益相关者(这是通过复选框完成的)

例如,一个名为John Doe的人属于6/8复选框,上面的代码将所有信息传输到6/8的复选框。但是我始终希望这些信息在主选项卡中填写,并带有附加的列名借记相关者,该名称将传输选中框的名称。当我尝试它时,它对每个复选框进行了单独的行,而不是将其编译成一个单元格。因此,我用相同的信息制作了6个约翰·多伊(John Doe),但每个约翰·多伊(John Doe)都有不同的利益相关者。

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

sub copypastedata()dim strsourcesheet作为字符串dim strdestinationheet作为字符串dim lastrow as Long

strsourcesheet ="数据输入"

sheet(strsourcesheet).visible = true sheet(strsourcesheet).SELECT

range(" C2")。选择do wher activecell.value<>" strdestinationheet= ActiveCell.Value ActiveCell.Offset(0,-2).Resize(1,ActiveCell.CurrentRegion.Columns.Counts.Counts.count).select Selection.copy Sheets(strdestinationationsheet).visible = true sheets = true sheets(strdestinationsheet)。a")单元格(lastrow 1,1)。选择选择。Pastecialxlpastevalues application.cutcopymode = false sheets(strsourceSheet).select activecell.offecell.offset.offset(0,2).select activecell.overecell.offecell.offset.offset.offset.offset(1,0).SELECT LOOP LOOP LOOP END ENDEND ENDENsub

最新更新