多张工作表的WorksheetFunction.CountIf

时间:2019-09-09 10:46:47

标签: excel vba

我正在使用WorksheetFunction.CountIf在两个不同的工作表之间进行比较,但出现1004错误

Sub sbWriteIntoCellData()
Dim CODE As Workbook
Dim Sheet1 As Sheets
Dim Sheet2 As Sheets
Set Sheet1 = Sheets
Set Sheet2 = Sheets
For Each rngCell In Worksheets("Sheet1").range("A2", range("A2").End(xlDown))

If WorksheetFunction.CountIf(Sheets("Sheet2").range("A2", range("A2").End(xlDown)), rngCell) = 1 Then

      Worksheets("Sheet1").range("C" & Rows.Count).End(xlUp).Offset(1) = "Yes"
        Else: Worksheets("Sheet1").range("C" & Rows.Count).End(xlUp).Offset(1) = "No"
    End If



Next

MsgBox "Execution completed"

End Sub

[enter image description here

3 个答案:

答案 0 :(得分:0)

整理代码,使用正确的引用并正确设置变量。为每个Range对象指定其所在的工作表。这样您就不会出现任何错误(当然,我没有检查您的代码逻辑,只是纠正了明显的问题)。

Option Explicit

Public Sub sbWriteIntoCellData()
    Dim Sheet1 As Worksheet
    Set Sheet1 = ThisWorkbook.Worksheets("Sheet1")

    Dim Sheet2 As Worksheet
    Set Sheet2 = ThisWorkbook.Worksheets("Sheet2")

    Dim Cell As Range
    For Each Cell In Sheet1.Range("A2", Sheet1.Range("A2").End(xlDown))
        If Application.WorksheetFunction.CountIf(Sheet2.Range("A2", Sheet2.Range("A2").End(xlDown)), Cell.Value) = 1 Then
            Sheet1.Range("C" & Sheet1.Rows.Count).End(xlUp).Offset(1, 0) = "Yes"
        Else
            Sheet1.Range("C" & Sheet1.Rows.Count).End(xlUp).Offset(1, 0) = "No"
        End If
    Next Cell

    MsgBox "Execution completed"
End Sub

答案 1 :(得分:0)

我已经生成了将v-lookup公式传递到VBA的代码,并且运行良好... 我正在发布我的代码(已验证)。这可能会帮助正在寻找类似解决方案的人

该代码将用于比较工作表1中的三个不同行与处于条件中的工作表2中的另一行数据

简单来说:如果工作表1的F2 / H2 / I2中存在a2(工作表2)中的值,则代码将打印“是”。 结果将存储在工作表1的AB2中

    Sub compare()
    Dim DataRange As Range
      Dim Sheet1 As Worksheet
        Set Sheet1 = ThisWorkbook.Worksheets("Sheet1")
        Dim Sheet2 As Worksheet
        Set Sheet2 = ThisWorkbook.Worksheets("Sheet2")
.Find the last cell value
     Dim Cell As Range
      Dim last As Double
    With ActiveSheet
            last = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
       Range("AB2").Formula = "=IF(OR(ISNA(VLOOKUP(F2,Sheet2!$A$2:$A$5,1,FALSE))=FALSE,ISNA(VLOOKUP(H2,Sheet2!$A$2:$A$5,1,FALSE))=FALSE,ISNA(VLOOKUP(I2,Sheet2!$A$2:$A$5,1,FALSE))=FALSE),""Yes"",""No"")"
    Range("AB2").AutoFill Destination:=Range("AB2:AB" & last)
    MsgBox "Execution completed"
    End Sub

答案 2 :(得分:0)

使用R1C1表示法不仅可以增加处理时间,还可以使代码更易于维护。

Option Explicit

Public Sub sbWriteIntoCellData()

    Dim sht1 As Worksheet
    Set sht1 = ThisWorkbook.Worksheets("Sheet1")

    With sht1

        Dim lastRow As Long
        lastRow = .Range("A2").End(xlDown).Row

        Dim res As Range
        Set res = .Range("C2:C" & lastRow)

        With res
                                                 '-2 bc formula on column C and checking against column A from sheet2
            .FormulaR1C1 = "=If(COUNTIF(Sheet2!C[-2],RC[-2])>1,""Yes"",""No"")"
            .Calculate
            .Value = .Value
        End With

    End With

End Sub