vba-excel:如何使用for循环和if语句来排除复制一系列单元格

我有这个代码:

lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row 

(Sheet1.Cells(a,1)= Date or Date – 1)和(Sheet1.Cells(a,2)=“AA”或Sheet1.Cells(a,2)=“BB”或Sheet1.Cells(a,2)=“CC”)和Sheet1.Cells(a,3)= array(0)然后调用ActivateSheet Sheet1.Range(单元格(a,4),单元格(a,10))。复制Sheet2.Cells(Rows.Count,1).End(xlUp).Offset(1,0).PasteSpecial End If Next a

我有两列数据在其中。 在A列中我们有DATE的值,我将调用currentDate和DATE-1的值,我将调用yesterdayDate 。 在B列中,我可以有三个不同的值,分别是AABBCC 。 我上面的if语句(我很抱歉代码看起来不怎么样,我仍然试图学习VBA; P)基本上检查列A中的值是currentDate还是yesterdayDate,并检查列B中的值是AABB ,或CC 。 那么,如果列A和列B的值是给定值的任意组合,则它将复制该单元格的范围并将其粘贴到Sheet2上

所以这就是我想要发生的事情。 从给定值的所有可能的组合中,有一个组合我不想复制,并且这个组合是yesterdayDate && CC

我只想要我的代码复制yesterdayDate && AAyesterdayDate && BBcurrentDate && CC 。 所有其他组合,如currentDate && AA或currentDate && BB将不可能发生根据用户input。

我只想排除yesterdayDate && CC被我的代码复制。 任何想法,我怎么能做到这一点?

在这里,我给你一个。 试试这个。

 Public Sub checkAndCopy() Dim rowCount, row As Integer Dim dateCell, valueCell, combinationCell As String Dim isValid As Boolean 'Getting row count from Sheet1 rowCount = Sheet1.Cells(Rows.Count, 1).End(xlUp).row 'Looping all row from Sheet1. For row = 1 To rowCount 'getting cell values dateCell = Sheet1.Range("A" & row) valueCell = Sheet1.Range("B" & row) combinationCell = Sheet1.Range("C" & row) 'Sometime one of these cell should be blank or wrong date value. 'So, I added checking to avoid it. 'If these two cell are not empty, check date is valid or not. If dateCell <> "" And valueCell <> "" Then 'If date value is valid, go on checking for copy cell. If IsDate(dateCell) Then 'Reset isValid flag. isValid = True 'You just want to exclude yesterday & CC. 'So, I only check for it. If dateCell = Date - 1 And valueCell = "CC" Then isValid = False End If 'If both cell values are valid and also combination cell is valid, copy and paste cell. If isValid And combinationCell = array(0) Then 'Select cells Sheet1.Range(Cells(row, 4), Cells(row, 10)).Select 'Copy cells Selection.Copy 'Paste cells Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial 'Reset clipboard Application.CutCopyMode = False End If End If End If Next row End Sub