Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 28

Thread: Solved: VBA code to insert a formula

  1. #1
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location

    Question Solved: VBA code to insert a formula

    What's the VBA code to insert a formula in cells I2:I500?.

    Thanks,

    Marcster.

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Sub InsertFormula()

    Application.Calculation = xlCalculationManual

    Range("A1").FormulaR1C1 = "Your formula here"

    Range("A1").Copy Destination:=Range("I2:I500")

    Range("I2:I500").PasteSpecial Paste:=xlPasteFormulas

    Application.Calculation = xlCalculationAutomatic

    End Sub
    Peace of mind is found in some of the strangest places.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Marcster
    What's the VBA code to insert a formula in cells I2:I500?.

    Thanks,

    Marcster.
    Range("I2:I500").Formula = yourformula

  4. #4
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hey Marcster,

    Although it could change depending on the actual formula being entered...[vba]Range("I2:I500").Formula = "=1+1"[/vba]Matt

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Austenr,

    Why turn off calculation before entering the formula?

    Also, you might find it easier to use a format like[vba]Sub InsertFormula()
    With Range("I2:I500")
    .FormulaR1C1 = "Your formula here"
    .Value = .Value
    End If
    End Sub[/vba]Matt

  6. #6
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location
    Thanks for all the replies guys .

    Another thread solved.


    Marcster.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mvidas
    Also, you might find it easier to use a format like[vba]Sub InsertFormula()
    With Range("I2:I500")
    .FormulaR1C1 = "Your formula here"
    .Value = .Value
    End If
    End Sub[/vba]Matt
    He isn't pasting values.

  8. #8
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Ha! Goes to show you that reading isnt exactly overrated

    I suppose I just dont get it then

  9. #9
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    OOPS....
    Peace of mind is found in some of the strangest places.

  10. #10
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location
    Along those lines...

    I built something for myself that instantly converts any cell formula to the R1C1 VBA syntax. Granted, not particularly difficult, but handy automating the drudge task; particularly if you're dealing with a lot of double quotes in the formulas that have to be doubled up for VBA usage.

    Anyway... I figured a few of the reg'lars over here might appreciate it. It's something I keep on a button in my own toolbox.

    [VBA]
    Sub Get_VBA_Formula()

    Dim VBA_Formula As String, n$, x$, msg As String
    Dim i As Integer

    VBA_Formula = ActiveCell.FormulaR1C1Local

    'double quote substitution
    For i = 1 To Len(VBA_Formula)
    n$ = Mid(VBA_Formula, i, 1)
    If n$ = """" Then
    x$ = x$ & """"""
    Else
    x$ = x$ & n$
    End If
    Next i

    'Post formula to InputBox
    VBA_Formula = """" & x$ & """"
    msg = "Cell Formula to VBA Conversion" & vbCrLf & vbCrLf & ActiveCell.Formula
    Application.SendKeys ("^c")
    VBA_Formula = InputBox(msg, "Get VBA Formula", VBA_Formula)

    End Sub
    [/VBA]

    Note: after you press the OK button on the input box that pops up, the formula is automatically copied to the clipboard. So all you need to do is close the inputbox and go paste into your VBA FormulaR1C1 line.


  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by Aaron Blood
    I built something for myself that instantly converts any cell formula to the R1C1 VBA syntax. Granted, not particularly difficult, but handy automating the drudge task; particularly if you're dealing with a lot of double quotes in the formulas that have to be doubled up for VBA usage.
    LOL!

    And here I always do it the hard way... I like it!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  12. #12
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Since we are dealing with formulas and some of us are more slow than others (me), how can i suppress the #N/A using iserror inthis formula:

    =INDEX($A:$A,MATCH(C1,$A:$A,0))

    Thanks
    Peace of mind is found in some of the strangest places.

  13. #13
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Austenr,

    =IF(ISERROR(INDEX($A:$A,MATCH(C1,$A:$A,0))),"",INDEX($A:$A,MATCH(C1,$A:$A,0 )))

    Btw - Me saying "reading isnt exactly overrated" earlier wasn't negative towards you, I was saying I apparently hadnt fully read your code and just assumed you were pasting the values based on glancing at it .. my fault, not yours

  14. #14
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thats ok. Thanks for clearing up the formula. So in escence, you need to do the INDEX and MATCH 2 times with a " " seperator. Now I see.
    Peace of mind is found in some of the strangest places.

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by austenr
    Thats ok. Thanks for clearing up the formula. So in escence, you need to do the INDEX and MATCH 2 times with a " " seperator. Now I see.
    No, he is just testing to see whether that formula errors, if so, out put a space, otherwise return the form ula result.

  16. #16
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    I have also problem with inserting formula
    [VBA]
    Sheets("Koond").Range("H10:H" & lRow + 8).Formula = "=SUM(abileht!A10:A30)"
    'Work but
    Sheets("Koond").Range("H10:H" & lRow + 8).Formula = "=SUMIF(abileht!$I$2:$I$600;C10;abileht!$C$2:$C$600)"
    'Dosn't work and got run-time error 1004
    '(NB! in my location is inside formula ; not ,)
    [/VBA]

  17. #17
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Cass,

    That's interesting... I've never seen the ; as a separator before. I copied your code, changed the ; to , and ran it with no issue here. I wonder if the VBE needs it written in English to interpret it. Have you tried this?

    [vba]Sheets("Koond").Range("H10:H" & lrow + 8).Formula = "=SUMIF(abileht!$I$2:$I$600,C10,abileht!$C$2:$C$600)"[/vba]

    Also, you're sure that you're sheets are named correctly and that "Koond" is not a chart sheet, yes?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  18. #18
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    Wonderful All the sheets name is correct. If i change the separators , then it's work. But if i look now formula in worksheet there are ; separators

  19. #19
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Great! Glad it worked!

    It's interesting to me that MS would change the formula condition separator in the Excel interface. I see you're from Estonia, what language Excel are you using?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  20. #20
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    Quote Originally Posted by kpuls
    Great! Glad it worked!

    It's interesting to me that MS would change the formula condition separator in the Excel interface. I see you're from Estonia, what language Excel are you using?
    English version Office and Windows but regional settings in OS are Estonian and there are the differences

Posting Permissions

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