+ Reply to Thread
Results 1 to 3 of 3

How to insert a relative dynamic formula in VBA

  1. #1
    Registered User
    Join Date
    10-11-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    How to insert a relative dynamic formula in VBA

    Hello,

    I have a macro that imports a bunch a impedance numbers from each text file into their own column in the active worksheet, and then inserts some equations into other cells of the same column to show things such as averages, max, min, etc. Since each text file has a different amount of impedance numbers, the equations need to be relative to the column where they are located and also dynamic to cover the correct number of cells in their row. Here is an example of a working formula:

    cl.Offset(2, 1).FormulaR1C1 = "=AVERAGE(R[+3]C:R[+100]C)"

    Here is an example of what I would like to do:

    cl.Offset(2, 1).FormulaR1C1 = "=AVERAGE(R[+3]C:R[+j]C)"

    I have a loop just prior to this line of code that inserts the impedance numbers one at a time, so the loop counter (j) contains the amount of rows that the average equation needs to cover. VBA doesn't seem to like it when I replace the 100 with a variable. What is the correct way to do this? Thanks,

    Adrian

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to insert a relative dynamic formula in VBA

    Hi,

    Try
    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    10-11-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: How to insert a relative dynamic formula in VBA

    Oh I see, it's just like what you do in a msgbox, that makes sense. This works perfectly, thanks for the help Richard.

+ 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