+ Reply to Thread
Results 1 to 2 of 2

Help on combining declared variable and cell formula

  1. #1
    sylink
    Guest

    Help on combining declared variable and cell formula

    Am new in macro. i encountered runtime error trying to develop a sub to do
    same work as SUBTOTAL. I had problems on the line the line that has a
    combination of declared variable and cell formula: ActiveCell.FormulaR1C1 =
    "=RC[-1]+ TOT" Below is the full code.


    Dim z As Long
    Dim TOT As Double
    z = 3
    Range("C2").FormulaR1C1 = "=RC[-1]"
    TOT = Range("C2").Value

    Do While Range("A" & z) <> ""

    If Range("A" & z).Value = Range("A" & z - 1).Value Then

    'Range("C" & z - 1).FormulaR1C1 = "=RC[-1]+R[-1]C"

    Range("C" & z).Select
    ActiveCell.FormulaR1C1 = "=RC[-1]+ TOT"

    TOT = "=RC[-1]" + TOT
    Range("C" & z - 1).FormulaR1C1 = 0

    Else
    Range("A" & z).Select
    ActiveCell.FormulaR1C1 = 0

    End If
    z = z + 1
    Loop

  2. #2
    justahelper
    Guest

    RE: Help on combining declared variable and cell formula

    use
    activecell.formula = activecell.offset(0,-1) + TOT
    instead of
    ActiveCell.FormulaR1C1 = "=RC[-1]+ TOT"


    also use
    TOT = activecell.offset(0,-1) + TOT
    instead of
    TOT = "=RC[-1]" + TOT

    "sylink" wrote:

    > Am new in macro. i encountered runtime error trying to develop a sub to do
    > same work as SUBTOTAL. I had problems on the line the line that has a
    > combination of declared variable and cell formula: ActiveCell.FormulaR1C1 =
    > "=RC[-1]+ TOT" Below is the full code.
    >
    >
    > Dim z As Long
    > Dim TOT As Double
    > z = 3
    > Range("C2").FormulaR1C1 = "=RC[-1]"
    > TOT = Range("C2").Value
    >
    > Do While Range("A" & z) <> ""
    >
    > If Range("A" & z).Value = Range("A" & z - 1).Value Then
    >
    > 'Range("C" & z - 1).FormulaR1C1 = "=RC[-1]+R[-1]C"
    >
    > Range("C" & z).Select
    > ActiveCell.FormulaR1C1 = "=RC[-1]+ TOT"
    >
    > TOT = "=RC[-1]" + TOT
    > Range("C" & z - 1).FormulaR1C1 = 0
    >
    > Else
    > Range("A" & z).Select
    > ActiveCell.FormulaR1C1 = 0
    >
    > End If
    > z = z + 1
    > Loop


+ 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