簡體   English   中英

復制和粘貼具有異常的Excel單元格-VBA

[英]Copy and Paste Excel Cells With Exceptions - VBA

我有一個帶有復選框的excel文件,當選中復選框時,當鏈接的單元格為TRUE時,VBA代碼將復制該行中的所需單元格。 請參見下面的示例。

[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的所有其他實例。

類似於以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM