+ Reply to Thread
Results 1 to 7 of 7

How to set formula with cell reference with a variable

  1. #1
    momotaro
    Guest

    How to set formula with cell reference with a variable


    I would like to set formula that includes a string variable in VBA. The
    following
    code caused "Runtime error 1004 Application-defined or Object-defined error"

    dim strRow_Number as String
    strRow_Number = "100"

    ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number & "]C[-2])"

    How can I set the formula with variables???




  2. #2
    K Dales
    Guest

    RE: How to set formula with cell reference with a variable

    The syntax of the code is correct. Is the sheet protected? If the active
    cell is protected it would give the 1004 error.
    --
    - K Dales


    "momotaro" wrote:

    >
    > I would like to set formula that includes a string variable in VBA. The
    > following
    > code caused "Runtime error 1004 Application-defined or Object-defined error"
    >
    > dim strRow_Number as String
    > strRow_Number = "100"
    >
    > ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number & "]C[-2])"
    >
    > How can I set the formula with variables???
    >
    >
    >


  3. #3
    STEVE BELL
    Guest

    Re: How to set formula with cell reference with a variable

    I think you just need to your variable as a number type (double, long,
    integer, etc)
    dim strRow_Number as Double
    strRow_Number = 100
    than
    ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number & "]C[-2])"
    --
    steveB

    Remove "AYN" from email to respond
    "momotaro" <[email protected]> wrote in message
    news:[email protected]...
    >
    > I would like to set formula that includes a string variable in VBA. The
    > following
    > code caused "Runtime error 1004 Application-defined or Object-defined
    > error"
    >
    > dim strRow_Number as String
    > strRow_Number = "100"
    >
    > ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number & "]C[-2])"
    >
    > How can I set the formula with variables???
    >
    >
    >




  4. #4
    momotaro
    Guest

    Re: How to set formula with cell reference with a variable

    That was the answer, Steve!
    IT worked, Thank you.


    "STEVE BELL" wrote:

    > I think you just need to your variable as a number type (double, long,
    > integer, etc)
    > dim strRow_Number as Double
    > strRow_Number = 100
    > than
    > ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number & "]C[-2])"
    > --
    > steveB
    >
    > Remove "AYN" from email to respond
    > "momotaro" <[email protected]> wrote in message
    > news:[email protected]...
    > >
    > > I would like to set formula that includes a string variable in VBA. The
    > > following
    > > code caused "Runtime error 1004 Application-defined or Object-defined
    > > error"
    > >
    > > dim strRow_Number as String
    > > strRow_Number = "100"
    > >
    > > ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number & "]C[-2])"
    > >
    > > How can I set the formula with variables???
    > >
    > >
    > >

    >
    >
    >


  5. #5
    STEVE BELL
    Guest

    Re: How to set formula with cell reference with a variable

    Glad it was a simple fix... Good to hear it is working...

    And You are Very Welcome!

    Keep on Exceling...

    --
    steveB

    Remove "AYN" from email to respond
    "momotaro" <[email protected]> wrote in message
    news:[email protected]...
    > That was the answer, Steve!
    > IT worked, Thank you.
    >
    >
    > "STEVE BELL" wrote:
    >
    >> I think you just need to your variable as a number type (double, long,
    >> integer, etc)
    >> dim strRow_Number as Double
    >> strRow_Number = 100
    >> than
    >> ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number &
    >> "]C[-2])"
    >> --
    >> steveB
    >>
    >> Remove "AYN" from email to respond
    >> "momotaro" <[email protected]> wrote in message
    >> news:[email protected]...
    >> >
    >> > I would like to set formula that includes a string variable in VBA. The
    >> > following
    >> > code caused "Runtime error 1004 Application-defined or Object-defined
    >> > error"
    >> >
    >> > dim strRow_Number as String
    >> > strRow_Number = "100"
    >> >
    >> > ActiveCell.FormulaR1C1 = "=SUM(R[1]C[-2]:R[" & strRow_Number &
    >> > "]C[-2])"
    >> >
    >> > How can I set the formula with variables???
    >> >
    >> >
    >> >

    >>
    >>
    >>




  6. #6
    Registered User
    Join Date
    01-30-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How to set formula with cell reference with a variable

    A similar problem and I guess very basic problem -

    I have a huge worksheet which I created in excel 2010 and I used sumifs rather rather liberally.
    The end-user has excel 2003 and sumifs doeesn't work there.

    I understand that I can use sumproduct instead of sumifs but it won't be practical to go and change formula in close to 4000 cells.

    The good things is that in every sumifs there is only one criteria range, so I was wondering if I could write a UDF of SUMIFS and then use SUMIF function to calculate the value.

    I wrote the following code but it doesn't work.

    Please Login or Register  to view this content.
    Any help would be much appreciated !
    Last edited by arlu1201; 02-13-2013 at 04:19 AM. Reason: Use code tags.

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to set formula with cell reference with a variable

    Rahul,

    Welcome to the Forum.

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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