简体   繁体   English

vba-excel:如何使用for循环和if语句排除复制单元格区域

[英]vba-excel: how to exclude copying a range of cells with for loop and if statement

I have this code: 我有以下代码:


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

For a = 1 To lr If (Sheet1.Cells(a, 1) = Date Or Date - 1) And (Sheet1.Cells(a, 2) = "AA" Or Sheet1.Cells(a, 2) = "BB" Or Sheet1.Cells(a, 2) = "CC") And Sheet1.Cells(a, 3) = array(0) Then Call ActivateSheet Sheet1.Range(Cells(a, 4), Cells(a, 10)).Copy Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial End If Next a

I have two columns with data in them. 我有两列包含数据。 In column A we have there the value of DATE which i'll call currentDate and the value of DATE-1 which i'll call yesterDate . A列中,有DATE的值(我将其称为currentDate)和DATE-1的值(将其称为yesterDate) In column B I could have three different values, which are AA , BB , and CC . B列中,我可以具有三个不同的值,即AABBCC My if statement above (i apologize for how noobish the code looks, i'm still trying to learn VBA. ;P) basically checks if the values in column A are either the currentDate OR yesterDate and checks if the values in column B are either AA , BB , OR CC . 上面的我的if语句(我为代码看起来不太合理而道歉,我仍在尝试学习VBA。; P)基本上检查A列中的值是currentDate还是yesterDate,并检查B列中的值是AABBCC THEN, if the values of column A and column B are any combinations of the given values, it will copy that range of cells and paste it on Sheet2 . 然后,如果列A和列B的值是给定值的任何组合,它将复制该单元格范围并将其粘贴到Sheet2上

So here's what I want to happen. 所以这就是我想要发生的事情。 From all the possible combinations of the values given, there's one combination I don't want to copy and that combination is yesterDate && CC . 从给定值的所有可能组合中,有一个我不想复制的组合,而该组合是yesterDate && CC

I only want my code to copy yesterDate && AA , and yesterDate && BB , and currentDate && CC . 我只希望我的代码复制yesterDate && AAyesterDate && BBcurrentDate && CC All other combinations like currentDate && AA or currentDate && BB will not likely to happen based from the user input. 根据用户输入,不太可能发生所有其他组合,例如currentDate && AA或currentDate && BB

I just want to exclude yesterDate && CC to be copied by my code. 我只想排除yesterDate && CC由我的代码复制。 Any ideas on how I can achieve this? 关于如何实现此目标的任何想法?

Here, I got one for you. 在这里,我给你一个。 Try with this. 试试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM