【问题标题】:Checkbox to copy selected cells only and paste to another worksheet复选框仅复制选定的单元格并粘贴到另一个工作表
【发布时间】:2021-10-22 23:16:46
【问题描述】:

我在这方面不是很先进,但是我希望获得一些方向。我目前正在运行以下 VBA:

Private Sub CommandButton1_Click()

If (CheckBox1.Value = True) Then
        ActiveSheet.Range("B13:E18").Copy
    End If

If (CheckBox2.Value = True) Then
        ActiveSheet.Range("B20:E25").Copy
    End If

If (CheckBox3.Value = True) Then
        ActiveSheet.Range("B27:E32").Copy
    End If
    
If (CheckBox4.Value = True) Then
        ActiveSheet.Range("B34:E39").Copy
    End If

    'copy the chunk above for more check boxes

End Sub

但是,它最终只会复制最后一个选中的复选框,而不是一次复制多个单元格。为了每个复选框仅复制选定的单元格并将它们复制到同一工作簿中的另一个工作表,我缺少什么?

【问题讨论】:

  • 如果意图是复制 B13:E18 和 B20:E25 等,您需要使用 Union 来构造一个包含您想要的每个目标范围的范围。然后在“联合”范围内进行复制。
  • 此代码是在用户窗体上还是在工作表代码模块中?
  • @Tim Williams:他们说它基本上可以工作,所以必须在用户表单中,不是吗?
  • 可能是工作表...
  • @Tim Williams:好的。必须因 excel 版本而异。我无法将旧版本 (2010) 中的代码作为工作表中的代码运行。它仅适用于用户表单。为了我的启蒙,我将在适当的版本中进行测试。你能告诉我它适用于什么版本吗?

标签: excel vba copy cell worksheet


【解决方案1】:

这是一个粗略但有效的例子:

Public Sub CommandButton1_Click()

    Dim rgCopy As Range
    
    With ActiveSheet
        If CheckBox1 Then
            Set rgCopy = .Range("B13:E18")
        End If
        
        If CheckBox2 Then
            If rgCopy Is Nothing Then
                Set rgCopy = .Range("B20:E25")
            Else
                Set rgCopy = Union(rgCopy, .Range("B20:E25"))
            End If
        End If
        
        If CheckBox3 Then
            If rgCopy Is Nothing Then
                Set rgCopy = .Range("B27:E32")
            Else
                Set rgCopy = Union(rgCopy, .Range("B27:E32"))
            End If
        End If
        
        If CheckBox4 Then
            If rgCopy Is Nothing Then
                Set rgCopy = .Range("B34:E39")
            Else
                Set rgCopy = Union(rgCopy, .Range("B34:E39"))
            End If
        End If
    End With
    
    If Not rgCopy Is Nothing Then
        rgCopy.Copy
    Else
        MsgBox "nothing selected message"
    End If

End Sub

【讨论】:

    【解决方案2】:

    根据复选框的值复制范围

    标准模块,例如Module1

    Option Explicit
    
    Sub CopyChkBoxConsecutiveRanges(ByVal chkBoxes As Variant)
        
        ' Source
        Const sName As String = "Sheet1"
        Const sfrgAddress As String = "B13:E18"
        Const sGap As Long = 1
        ' Destination
        Const dName As String = "Sheet2"
        Const dfCellAddress As String = "A2"
        ' Workbook
        Dim wb As Workbook: Set wb = ThisWorkbook
        
        ' Source
        Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
        Dim srg As Range: Set srg = RefChkBoxConsecutiveRanges( _
            sws.Range(sfrgAddress), chkBoxes, sGap)
        'Destination
        Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
        Dim dfCell As Range: Set dfCell = dws.Range(dfCellAddress)
        
        ' Copy
        If Not srg Is Nothing Then
            srg.Copy dfCell
        End If
        
    End Sub
    
    Function RefChkBoxConsecutiveRanges( _
        ByVal sfrg As Range, _
        ByVal chkBoxes As Variant, _
        Optional ByVal sGap As Long = 0, _
        Optional ByVal SearchOrder As XlSearchOrder = xlByRows) _
    As Range
    ' Needs `RefCombinedRange`.
        
        Dim sws As Worksheet: Set sws = sfrg.Worksheet
        Dim srOffset As Long
        srOffset = IIf(SearchOrder = xlByRows, sfrg.Rows.Count + sGap, 0)
        Dim scOffset As Long
        scOffset = IIf(SearchOrder = xlByRows, 0, sfrg.Columns.Count + sGap)
        Dim scrg As Range: Set scrg = sfrg
            
        Dim srg As Range
        Dim n As Long
        
        For n = LBound(chkBoxes) To UBound(chkBoxes)
            If chkBoxes(n) Then
                Set srg = RefCombinedRange(srg, scrg)
            End If
            Set scrg = scrg.Offset(srOffset, scOffset)
        Next n
        
        If Not srg Is Nothing Then
            Set RefChkBoxConsecutiveRanges = srg
        End If
        
    End Function
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Purpose:      Creates a reference to a range combined from two ranges.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function RefCombinedRange( _
        ByVal CombinedRange As Range, _
        ByVal AddRange As Range) _
    As Range
        If CombinedRange Is Nothing Then
            Set RefCombinedRange = AddRange
        Else
            Set RefCombinedRange = Union(CombinedRange, AddRange)
        End If
    End Function
    

    用户表单模块,例如UserForm1

    Private Sub CommandButton1_Click()
        Dim chkBoxes As Variant
        chkBoxes = Array(CheckBox1, CheckBox2, CheckBox3, CheckBox4) ' add more
        CopyChkBoxConsecutiveRanges chkBoxes
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 2021-11-18
      • 2016-08-15
      相关资源
      最近更新 更多