+ Reply to Thread
Results 1 to 4 of 4

VBA Range.Formula with IF statement

Hybrid View

  1. #1
    Registered User
    Join Date
    06-24-2020
    Location
    Philadelphia, PA
    MS-Off Ver
    Office 365
    Posts
    28

    VBA Range.Formula with IF statement

    Hi Everyone -

    mKounter = 6
    Range("M" & mKounter).Formula = "=If(mKounter=6,=counta("M6:M6"),=counta("M6:M" &mkounter - 1))"

    Trying to have VBA enter a count formula, depending on the value of mKounter. If there are no data rows, it should be M6:M6. If data rows are present, then count all rows, M6:M (whatever mKounter happens to be).

    Getting: Expected: end of statement error message.

    Appreciate any guidance on this.

    Thanks,
    Sluggy

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA Range.Formula with IF statement

    You need to use code tags around code - it's hard to read otherwise. Regardless, try this:

    Range("M" & mKounter).Formula = IIf(mKounter <= 6, "=COUNTA(M6:M6)", "=COUNTA(M6:M" & mKounter - 1 & ")")
    Note that mKounter = 6 creates a self-referencing formula and will probably produce an error.

    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Registered User
    Join Date
    06-24-2020
    Location
    Philadelphia, PA
    MS-Off Ver
    Office 365
    Posts
    28

    Re: VBA Range.Formula with IF statement

    Thanks WideBoyDixon! Appreciate the help!

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,427

    Re: VBA Range.Formula with IF statement

    Maybe this:

    Option Explicit
    
    Sub Test()
    
    Dim mKounter As Long
    mKounter = 6
    
    Range("M" & mKounter).Formula = "=If(" & mKounter & "=6, counta(M6:M6), counta(M6:M" & mKounter - 1 & "))"
    
    ' >>>>   =IF(6=6, COUNTA(M6:M6), COUNTA(M5:M6))
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Applying formula containing if statement to a range using VBA
    By thebeastslayer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-15-2020, 08:33 AM
  2. Replies: 1
    Last Post: 05-04-2018, 01:02 PM
  3. Nested If statement formula not working for range of values
    By michelle3631 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-11-2018, 08:36 PM
  4. [SOLVED] Replace formula in range of cells (with/without if then statement)
    By bjprent in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2016, 08:56 AM
  5. [SOLVED] IF statement for Sheet.Range.Formula
    By shre0047 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2016, 10:57 AM
  6. [SOLVED] Add IF Statement formula to cells in varying range and paste to used range
    By Webbers in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-26-2015, 01:28 PM
  7. Replies: 1
    Last Post: 11-07-2014, 01:42 PM

Tags for this Thread

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