Consulting

Results 1 to 3 of 3

Thread: Countif function on varying ranges

  1. #1
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    2
    Location

    Countif function on varying ranges

    I need to fill the number of times a member appears in the data at the end of the table, However the sizes of the tables varry
    How can i have the countif function work for the varying ranges
    Below is the code i am currently using

    'Visits on member sheet
    Sheets("Members").Select

    'Find last column
    Set fourthcell = Cells(2, 2)
    MemLastcol5 = Cells(fourthcell.Row, Columns.Count).End(xlToLeft).Column
    MemLastrow5 = Cells(Rows.Count, fourthcell.Column).End(xlUp).Row

    Range(Cells(3, MemLastcol5), Cells(MemLastrow5 - 1, MemLastcol5)).Select

    Selection.FormulaR1C1 = _
    "=+COUNTIF(Visits!R2C8:R69C8,Members!RC[-5]"

    The member number column is sometimes 5 cells back or 6,3 maybe 2 so i need to make the countif function to varry to accomodate that

    Any help rendered will be appreciated


    UTHMAN

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.

    post your workbook pls. (#2 in my forum signature)
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Newbie
    Joined
    Jun 2018
    Posts
    2
    Location
    Quote Originally Posted by mancubus View Post
    welcome to the forum.

    post your workbook pls. (#2 in my forum signature)
    I was able to get a solution as below

    We just use a loop to ensure it works just right
    Dim op_id As String
    For i = 3 To MemLastrow5

    Op_id = Sheets("Members").Cells(i, 2)
    Sheets("Members").Cells(i, MemLastcol5) = _
    "=COUNTIF('Visits'!H:H, """ & Op_id & """)"

    Next i

Posting Permissions

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