Excel Macro: Error 9, If x Countif

Asked 2 years ago, Updated 2 years ago, 77 views

How do I resolve the error code '9'?Below is the If(WorksheetFunction.CountIf part.

Sub Downloaded()

    n=Cells(Rows.Count, "F").End(xlUp).Row
    Dimi

    Fori=2 Ton

        If Range("o" & i) = 0 Then
            Range("a"&i).Value=""
        Else
            Range("a"&i).Value=Range("o"&i).Value
        End If

        ' Error on If statement below
        If(WorksheetFunction.CountIf(Worksheets(Unsubscribers), Range("A:A").Value) = 0) Then
            Range("b2").Value="Subscribed"
        Else
            Range("b2").Value="Unsubscribed"
        End If

    Next

End Sub

vba excel

2022-09-30 19:35

1 Answers

VBA error code 9 is "Index Out of Range" error.

WorksheetFunction.CountIf(Works (Unsubscribers), Range("A:A").Value

The Unsubscribers used in are not declared in the code of the question, nor are they substituted for values, so they are considered Empty.

Worksheets ("filename") is a worksheet object whose worksheet name is filename.
Worksheets (Empty) get the error "Index is not in valid range" because there is no worksheet named Empty.
== [That's it for the first part]============================

"The questioner commented, ""I would like to identify the one in column A of sheet A and the one in column A of sheet B, and display Yes/No in column B of sheet A."", so I would like to answer about the program instead of how to resolve the error."

== [Answer Part 2] ===========================

The code for the question does not show the corresponding objects for the two sheets (A and B).
In addition to columns A and B, columns F and O are used, but I didn't understand what I wanted to do.

Here is an example of a simplified code:
·ActiveSheet is Sheet A and Sheet B is Sheet Unsubscribers.
"·Check whether the value of the cell in column A of ""sheet A"" appears in the cell in column A of ""sheet B"", and write ""Yes"" or ""No"" in the cell on the right side of the value (column B)."

SubsampleCode()
    Dim sheetA as worksheet, sheetBas worksheet'worksheet Object Variables
    DimrowNo as Integer, rowMax as Integer' and last row of data

    SetsheetA=ActiveSheet
    SetsheetB=worksheets("Unsubscribers")

    rowMax=sheetA.Cells(Rows.Count, 1).End(xlUp).Row

    For RowNo=2 to rowMax
      If(WorksheetFunction.CountIf(sheetB.Range("A:A"),sheetA.Cells(rowNo,1))>0)Then'SheetB contains cell values in rowNo row A of rowA of sheetA
        sheetA.Cells(rowNo, 2) = "Yes" 'Write Yes to the right cell
      Else
        sheetA.Cells(rowNo, 2) = "No" 'Write No to the right cell
      End If
    next rowNo
End Sub

"The ""sheetA.""" part of the code can be moved without it, but I put it in to indicate which cell I handle."


2022-09-30 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.