Consulting

Results 1 to 2 of 2

Thread: COUNTIF Column based in VBA

  1. #1
    VBAX Regular
    Joined
    Feb 2020
    Posts
    8
    Location

    COUNTIF Column based in VBA

    I am trying to count number of 1 in every 5th column and write it in 7th line of that column. I got error Object required. I am beginner so probably I missed something important.

    Sub Countif()
    
      Dim wb As Workbook
      Set wb = ThisWorkbook
      Dim ws As Worksheet
      Set ws1 = wb.Sheets("Sheet2")
    
      Dim ValuesRange As Range
      Dim ResultCell As Range
      Dim CriteriaValue As String
      
      
      
      LastRow = ws1.Cells(ws1.Rows.Count, "J").End(xlUp).Row
      LastColumn = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
    
        For j = 10 To LastColumn Step 5
            For i = 2 To LastRow
    
                Set ValuesRange = Range("j8:j2197")
                Set ResultCell = cellRange.Cells(7, j)
    
                CriteriaValue = 1
    
                ResultCell = WorksheetFunction.Countif(ValuesRange, CriteriaValue)
    
            Next i
           
        Next j
       
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Probably 2 issues with


                Set ValuesRange = Range("j8:j2197")
                Set ResultCell = cellRange.Cells(7, j)

    1. Valuesrange will ALWAYS be col J
    2. cellRange is not Set (the error message)

    You probably wanted something like


                Set ValuesRange = Range(cells(8,j), cells(LastRow, j))
                Set ResultCell = Range.Cells(7, j)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •