【问题标题】:How to add information from vba to multiple sheets如何将信息从 vba 添加到多个工作表
【发布时间】:2014-01-28 18:16:22
【问题描述】:
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

根据检查的复选框,我只想将复选框的值编译到主选项卡和主选项卡中的一个单元格中。上面的代码根据该人所属的利益相关者传输每个文本框的值(这是通过复选框完成的)

例如,一个名叫 John Doe 的人属于 6/8 复选框,上面的代码将所有信息传输到 6/8 复选框表中。但我总是希望将信息填写在主选项卡中,并带有一个额外的列名称利益相关者,它将传输复选框的名称。当我尝试它时,它为每个复选框制作了单独的一行,而不是将其编译到一个单元格中。所以我用相同的信息制作了 6 个 John Doe,除了每个 John Doe 有不同的利益相关者他属于。

【问题讨论】:

    标签: vba excel userform


    【解决方案1】:

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

    Sub copyPasteData() Dim strSourceSheet As String Dim strDestinationSheet As String Dim lastRow As Long

    strSourceSheet = "数据输入"

    Sheets(strSourceSheet).Visible = True Sheets(strSourceSheet).Select

    Range("C2").Select Do While ActiveCell.Value "" strDestinationSheet = ActiveCell.Value ActiveCell.Offset(0, -2).Resize(1, ActiveCell.CurrentRegion.Columns.Count).Select Selection.Copy Sheets(strDestinationSheet).Visible = True Sheets(strDestinationSheet).Select 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).Select Loop End子

    【讨论】:

    • 请正确格式化您的代码(选择并按 ctrl-k 缩进 4 个空格并触发格式化)。
    猜你喜欢
    • 2023-02-02
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2017-01-08
    相关资源
    最近更新 更多