【问题标题】:Copy and Paste Excel Cells With Exceptions - VBA复制和粘贴有例外的 Excel 单元格 - VBA
【发布时间】:2018-07-12 20:31:57
【问题描述】:

我有一个带有复选框的Excel文件,当勾选复选框时,VBA代码将在链接的单元格状态为true时复制该行中所需的单元格。请参阅下面的示例。

[Private Sub CommandButton1_Click()
Dim lastrow As Long, erow As Long
'to check the last filled row on sheet named one
lastrow = Worksheets("one").Cells(Rows.Count, 2).End(xlUp).Row
For i = 2 To lastrow
If Worksheets("one").Cells(i, 4).Value = "TRUE" Then
Worksheets("one").Cells(i, 2).Copy
erow = Worksheets("two").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("one").Paste Destination:=Worksheets("two").Cells(erow + 1, 1)
Worksheets("one").Cells(i, 5).Copy
Worksheets("one").Paste Destination:=Worksheets("two").Cells(erow + 1, 2)
End If
Next i
End Sub]

但是我想知道是否有办法添加异常?因此,如果链接的单元格状态为 TRUE,而同一行中的另一个单元格状态为 NEVER,则不复制和粘贴该行,但仍粘贴 TRUE 所在的所有其他实例。

【问题讨论】:

  • 您可以在行上使用 Range.Find 方法来查看是否从未找到,如果没有,则复制
  • 你需要使用And操作符。 If linkedcell =TRUE and OtherCell <> NEVER then Copy......msdn.microsoft.com/en-us/vba/language-reference-vba/articles/…
  • 如果没有我认为应该是。因为你不希望它被发现。对不起。
  • @cyboashu 你的意思是 If Worksheets("one").Cells(i, 4).Value = "TRUE" and Worksheets("one").Cells(i, 2).Value = “从不”那么

标签: vba excel exception copy-paste


【解决方案1】:

类似于以下内容:

Option Explicit
Private Sub CommandButton1_Click()
    Dim lastrow As Long, erow As Long, i As Long
    Application.ScreenUpdating = False
    With Worksheets("one")
        lastrow = .Cells(Rows.Count, 2).End(xlUp).Row
        For i = 2 To lastrow
            If .Cells(i, 4).Value And .Rows(i).Find("NEVER", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
                 erow = Worksheets("two").Cells(Rows.Count, 1).End(xlUp).Row
                 Worksheets("two").Cells(erow + 1, 1) = .Cells(i, 2)
                 Worksheets("two").Cells(erow + 1, 2) = .Cells(i, 5)
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多