+ Reply to Thread
Results 1 to 3 of 3

Macro to add range & criteria in COUNTIFS Formula

Hybrid View

  1. #1
    Registered User
    Join Date
    12-28-2011
    Location
    FLORIDA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Macro to add range & criteria in COUNTIFS Formula

    I have over 500 countifs formulas on a single sheet, all are slightly different depending on row and column. I need a Macro that will insert a new range and new criteria into an existing COUNTIFS formula...

    Example:

    =COUNTIFS(game,"W",AH,"A")

    Changed to

    =COUNTIFS(game,"W",AH,"A",FD,"F")

    game, AH, FD are defined names of ranges on another sheet.

    If have this script below, but it just adds the new string to the end of the existing formula. I need the new string to be inside that last parenthesis. Or replace the last parenthesis with my new string. Help!

    Sub Macro3()
    
    Dim Orig_formula As String
    
    Orig_formula = ActiveCell.Formula
    Orig_formula = Mid(Orig_formula, 1, 1) & Mid(Orig_formula, 2) & "FD, " & Chr$(34) & "F" & Chr$(34) & ")"
    ActiveCell.Formula = Orig_formula
    
    End Sub
    Last edited by DonkeyOte; 12-28-2011 at 05:05 AM. Reason: CODE tags added (1st post)

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Macro to add range & criteria in COUNTIFS Formula

    Hello and welcome to the forum,

    Maybe something like this:

    ActiveCell.Formula = Left(ActiveCell.Formula, Len(ActiveCell.Formula) - 1) & "," & "FD," & """F""" & ")"
    Hope this helps.

    abousetta

    P.S. Please use code tags around your code. Details on how are available in the forum rules.
    Last edited by abousetta; 12-28-2011 at 03:36 AM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Macro to add range & criteria in COUNTIFS Formula

    Or use Replace:

    ActiveCell.Formula = Replace(ActiveCell.Formula, ")", "," & "FD," & """F""" & ")")
    abousetta

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1