Consulting

Results 1 to 8 of 8

Thread: Solved: Replace Cell with Formula - automatically

  1. #1

    Solved: Replace Cell with Formula - automatically

    When I run this sub it uses the formula in last line and paste's it throughout the sheet.

    HTML Code:
    Sub Replacecellwithformula()
     
    Range("G2:G25" & Columns("F").Find("A").Row).Formula = "=A2+D2"
    Range("G2:G25" & Columns("F").Find("B").Row).Formula = "=A2-B2"
     
    End Sub

    I am trying to get the results in the sheet attached.

    Kindly help.........

    Best regards

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub Replacecellwithformula()

    Range("G2:G25").Formula = "=IF(F2=""A"",A2+D2,IF(F2=""B"",A2-B2,""""))"

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Dear XLD,

    Thx for the reply, but I have about 50+ combination formulas which I want replace (automatically) based on the abrevation in Column F ("Formula Rng").

    Is there a way to go around this.

    Kindly guide....

    Thx-n-BR

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Can you go with Case Select?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub Replacecellwithformula()
    Dim LastRow As Long
    Dim i As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    .Range("G2").Resize(LastRow - 1).Value = ""

    .Columns("A:G").AutoFilter field:=6, Criteria1:="A"
    .Range("G2").Resize(LastRow - 1).SpecialCells(xlCellTypeVisible).Formula = "=A2+D2"

    .Columns("A:G").AutoFilter field:=6, Criteria1:="B"
    .Range("G2").Resize(LastRow - 1).SpecialCells(xlCellTypeVisible).Formula = "=A2-B2"

    'etc

    .Columns("A:G").AutoFilter field:=6
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Hi XLD,

    I tried ur sub - small problem

    When the sub run for the second criteria (eg, B) - it start again from cell A2.

    Eg, the B criteria starts from row 4 - but when the formula runs against the first row.

    Attached is the file for your review

    Kindly guide ...........

    Thanks

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    LOL! What a prat.

    [vba]

    Sub Replacecellwithformula()
    Dim LastRow As Long
    Dim i As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    .Range("G2").Resize(LastRow - 1).Value = ""

    .Columns("A:G").AutoFilter field:=6, Criteria1:="A"
    .Range("G2").Resize(LastRow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-6]+RC[-3]"

    .Columns("A:G").AutoFilter field:=6, Criteria1:="B"
    .Range("G2").Resize(LastRow - 1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[-6]-RC[-5]"

    'etc

    .Columns("A:G").AutoFilter field:=6
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Hey XLD - as always - UR the star! thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •