Consulting

Results 1 to 9 of 9

Thread: Solved: Formulas, entered by macro

  1. #1

    Solved: Formulas, entered by macro

    I had this problem long ago with formulas.
    Now I have it again with dates.

    I'm assigning a date (or formula) to a cell, but Excel doesn't see it like date/formula. To make it work i have to enter the formula line and press Enter key. After that formula starts to calculate and date becomes a date value and I can formate it like "April, 2007" or 01.01.2007, etc.

    I've tried to assign .Value property, to .Formula property
    Nothing helps

  2. #2
    Site Admin VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,005
    Location
    Vosmerkin, you need to provide the portion of code that you are using however you can format a date like:
    [VBA]
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("A1").Value = Format(Range("A1").Value, "dd mmm yyyy")
    End Sub
    [/VBA]if you were to use this and enter 22 12 07 in A1 the cell will display 22-Dec-2007 however if you entered it as a serial number 221207 (the number of days since 1900) then the cell will display 22-Aug-05.

    Hope this helps!

    Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    or
    [VBA] Range("A:A").NumberFormat = "dd.mm.yy"
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    try storing the value in a date variable in VBA.

  5. #5
    Now it's fine with dates.
    [VBA]CDate("01.01.2007")[/VBA]
    This works.

    But what to do with formulas?
    [VBA]Dim d As Variant
    d = "=сумм(A42+A41)"
    Range("a40").Select
    ActiveCell.Formula = d

    End Sub[/VBA]
    I get error in cell A40. To make formula work I have to go to the formula line and press ENTER key

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is this a udf, or a procedure? And what is cymm function in English?

  7. #7
    Sorry
    [vba]Sub macro1()

    Dim d As Variant
    d = "=summ(A42:A41)"
    Range("a40").Select
    ActiveCell.Formula = d

    End Sub[/vba]

  8. #8
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,289
    Location
    [vba]Sub macro_sum()
    Range("A40").Formula = "=SUM(A41:A42)"
    End Sub
    [/vba]The formula gets translated into the local excel version.

    Mine is a dutch version so SUM becomes SOM. But you'll have to watch out with , or ; when you need to use these in a formula or conditional formatting.

    Charlize

  9. #9
    Quote Originally Posted by Charlize
    [vba]Sub macro_sum()
    Range("A40").Formula = "=SUM(A41:A42)"
    End Sub
    [/vba]The formula gets translated into the local excel version.

    Mine is a dutch version so SUM becomes SOM. But you'll have to watch out with , or ; when you need to use these.

    Charlize

    It works. Thanks a lot.

Posting Permissions

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