Results 1 to 6 of 6

How to insert formulas using ranges in VBA?

Threaded View

  1. #1
    Registered User
    Join Date
    06-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    How to insert formulas using ranges in VBA?

    Is there a way to use VBA to insert the formula in R1C1 or Regular Reference format by using ranges? I need the formula to be present in Range2, not just the quotient.

    The formula (once inserted) will be autofilled to 10 rows down once it is done. As of now, this formula will only put the Numerical ANSWER (of the quotient)


    Code:

    Sub I_Am_Cool()
     
    Dim Range0 As Range, Range1 As Range, Range2 As Range, scol As Integer, srow As Integer
    srow = 1
    scol = 1
        Do While (Not IsEmpty(Cells(srow, scol)))
          scol = scol + 1
        Loop
    Set Range1 = Cells(srow, scol).Offset(2, -1)
    Set Range0 = Range1.Offset(0, 4)
    Set Range2 = Range1.Offset(3, 3)
    Range2.Formula = Range1 / Range0 
    End Sub
    When I do this formula, it just gives me the answer in numerical format. Also, if it was division by 0 (it will happen in the report), it gives a debug error.

    I have been searching forums and books for a week and cannot seem to find an answer.

    Any help would be greatly appreciated!

    Thanks!

    Just a note:

    I simplified the problem to make the answer easier. Range1 and Range2 will not always be the same cell (it's a rolling average spreadsheet).
    Last edited by envisioning; 06-22-2011 at 09:24 PM. Reason: Clarification

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