Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407

    set excel formula from access vba


    Hi, i am trying to create a cell formula using access vba

    the formula below works fine if i copy and paste it into the cell, but if i try to use access vba to put the value there it errors with application defined error.

    i have tried various ways of using quotes around sat and sun but cannot get it to go from access vba to an excel formula.

    this works when pasted into cell M8
    =IF(OR(B13="SAT",B13="SUN"),0, IF(J13>8,8,J13))

    this fails to put formula into the cell using access vba
    myformula = "=IF(OR(B13="SAT",B13="SUN"),0, IF(J13>8,8,J13))"
    Current_Worksheet.Range("M8").FormulaR1C1 = myformula

    Any help would be great thanks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Try using apostrophe instead of quote to delimit SAT and SUN.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    that was the first thing i tried, single, double, tripple.

    no go
    "=IF(OR(B8='SAT', B8='SUN'),0, IF(K8>8,8,K8))"

    this line
    "=IF(OR(B8=" & "SAT" & ", B8= & "SUN" & "),0, IF(K8>8,8,K8))"
    returns
    =IF(OR('B8'=SAT, 'B8'= SUN),0, IF('K8'>8,8,'K8'))
    with a name error

    still trying all sorts of combinations.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Excel certainly wants a quote mark.

    Not seeing:

    "=IF(OR(B8=""SAT"", B8=""SUN""),0, IF(K8>8,8,K8))"

    Maybe use Chr(34) somehow.

    Maybe change the spreadsheet to use day numbers instead of names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What is the code to open the Excel spreadsheet?

    Instead of a formula, can you put a number into a cell from Access? (this is to check if it is the formula or writing to Excel in general)

  6. #6
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    Quote Originally Posted by June7 View Post
    Excel certainly wants a quote mark.

    Not seeing:

    "=IF(OR(B8=""SAT"", B8=""SUN""),0, IF(K8>8,8,K8))"

    Maybe use Chr(34) somehow.

    Maybe change the spreadsheet to use day numbers instead of names.
    when i enter
    "=IF(OR(B8=""SAT"", B8=""SUN""),0, IF(K8>8,8,K8))"
    it puts the line below into the cell
    =IF(OR('B8'="SAT", 'B8'="SUN"),0, IF('K8'>8,8,'K8'))

    i will see if i can use day numbers to get around the syntax problem.

  7. #7
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    using numbers i get the same problem
    this line
    Current_Worksheet.Range("M8").FormulaR1C1 = "=IF(OR(B8=6, B8=7),0, IF(K8>8,8,K8))"

    puts this as the formula
    =IF(OR('B8'=6, 'B8'=7),0, IF('K8'>8,8,'K8'))

    i just can't see were the quotes need to be for this.

    all my other actions are fine, export to excel, format and layout the excell sheet.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    This is a very long shot, but try putting a space before and after the "=" sign and ">".
    Code:
    Current_Worksheet.Range("M8").FormulaR1C1 = "=IF(OR(B8 = 6, B8 = 7),0, IF(K8 > 8, 8, K8 ))"

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I just remembered I have VBA behind an Excel file that sets formulas into cells.
    Code:
        For intCCount = 2 To 7
            Cells(intRCount, intCCount + 1).Select
            ActiveCell = "=IF(Results!$" & strCol & "$" & intRow & "=0, " & Chr(34) & Chr(34) & ", Results!$" & strCol & "$" & intRow & ")"
            intRow = intRow + 1
        Next intCCount
        intRCount = intRCount + 40
        intCCount = 3
        Cells(intRCount, intCCount).Select
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    Quote Originally Posted by June7 View Post
    I just remembered I have VBA behind an Excel file that sets formulas into cells.
    Code:
        For intCCount = 2 To 7
            Cells(intRCount, intCCount + 1).Select
            ActiveCell = "=IF(Results!$" & strCol & "$" & intRow & "=0, " & Chr(34) & Chr(34) & ", Results!$" & strCol & "$" & intRow & ")"
            intRow = intRow + 1
        Next intCCount
        intRCount = intRCount + 40
        intCCount = 3
        Cells(intRCount, intCCount).Select
    thanks but i need to do it from access.
    i will just open a saved workbook that has a list of the formulas and copy/ paste them into my cells.

    thanks anyway.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I know you want to do it from Access. I thought the code syntax would be workable from Access as well. I did a test and got this to work:

    Dim oE As Excel.Application
    Dim oW As Excel.Workbook
    Dim oS As Excel.Worksheet
    Set oE = CreateObject("Excel.Application")
    Set oW = oE.Workbooks.Add
    oE.Visible = True

    ActiveCell = "=IF(OR(Sheet1!$B$8 = 6, Sheet1!$B$8 = 7), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"

    oW.Worksheets("Sheet1").Range("M8").Formula = "=IF(OR(Sheet1!$B$8 = 6, Sheet1!$B$8 = 7), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"

    This also worked:
    ActiveCell = "=IF($B$8=0, " & Chr(34) & Chr(34) & ", $K$8)"
    oW.Worksheets("Sheet1").Range("M8") = "=IF(OR(Sheet1!$B$8 = 6, Sheet1!$B$8 = 7), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"

    I don't think your use of FormulaR1C1 is correct:
    http://www.excelforum.com/excel-prog...mula-r1c1.html
    http://answers.microsoft.com/en-us/o...1-6151fbcaf20f
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    Quote Originally Posted by June7 View Post
    I know you want to do it from Access. I thought the code syntax would be workable from Access as well. I did a test and got this to work:

    Dim oE As Excel.Application
    Dim oW As Excel.Workbook
    Dim oS As Excel.Worksheet
    Set oE = CreateObject("Excel.Application")
    Set oW = oE.Workbooks.Add
    oE.Visible = True

    ActiveCell = "=IF(OR(Sheet1!$B$8 = 6, Sheet1!$B$8 = 7), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"

    oW.Worksheets("Sheet1").Range("M8").Formula = "=IF(OR(Sheet1!$B$8 = 6, Sheet1!$B$8 = 7), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"

    This also worked:
    ActiveCell = "=IF($B$8=0, " & Chr(34) & Chr(34) & ", $K$8)"
    oW.Worksheets("Sheet1").Range("M8") = "=IF(OR(Sheet1!$B$8 = 6, Sheet1!$B$8 = 7), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"

    I don't think your use of FormulaR1C1 is correct:
    http://www.excelforum.com/excel-prog...mula-r1c1.html
    http://answers.microsoft.com/en-us/o...1-6151fbcaf20f
    thanks again for trying to get this done, the above may work but i have found that i have to use Sat and Sun in place of the numbers 6 and 7 (yes i know it should be 7 and 1) as the rest of the sheet has this information already in it. The text in the formula then creates an application defined error.

    so anyway what i have decided is going to be the best is to use a formula sheet and copy the cells required into the excel report.
    i have never needed to open an extra copy of excel to do this before so i would like to get some help on opening a second workbook, select and copy the required cells and then paste the formulas from them into the first workbook and then close the reference workbook.
    the code i have so far is only sort of working. see sample below. hope you can help.


    first bit of code works fine, opens the workbook and does all the formatting i need.
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Employee Time Report Output", gg, True
    Set Excel_Workbook = GetObject(gg)
    Set Excel_Application = Excel_Workbook.Parent
    Excel_Application.WindowState = xlMinimized
    Excel_Application.Visible = True
    Excel_Workbook.Windows(1).Visible = True
    Excel_Workbook.Worksheets(1).Name = "Time Report Output"
    Excel_Workbook.Worksheets("Time Report Output").Tab.ColorIndex = 37
    Set Current_Worksheet = Excel_Workbook.Worksheets("Time Report Output")
    Excel_Workbook.Worksheets("Time Report Output").Select
    Current_Worksheet.Cells.Select
    With Selection
    Current_Worksheet.Cells.HorizontalAlignment = xlRight
    Current_Worksheet.Cells.Font.Name = "Times New Roman"
    End With
    continues to format sheet...

    this next bit is were i'm stuck.

    the workbook does open but isn't switching to the correct workbook to paste the cells

    Set Excel_Workbook1 = GetObject("c:\TimeTracK Formulas.xlsx")
    Set Excel_Application1 = Excel_Workbook1.Parent
    Excel_Application1.Visible = True
    Excel_Workbook1.Windows(1).Visible = True
    Current_Worksheet.Range("M8:P8").Copy
    Excel_Workbook1.Windows(1).Visible = False
    Set Current_Worksheet = Excel_Workbook.Worksheets("Time Report Output")
    Excel_Workbook.Worksheets("Time Report Output").Select
    Current_Worksheet.Range("M8:P8").PasteSpecial xlPasteFormulas

    PS if i don't have the formulas in the same cells as they are going to be pasted into then the formulas all change to various cell references all over the place, one changed to cell YV122

    also i can only put the $ sign in front of the column reference for the formulas as it is placed into a list and the rest of the fields update automatically with the correct row numbers.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    This works:

    oW.Worksheets("Sheet1").Range("M8") = "=IF(OR(Sheet1!$B$8 = " & Chr(34) & "SAT" & Chr(34) & ", Sheet1!$B$8 = " & Chr(34) & "SUN" & Chr(34) & "), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    trevor40's Avatar
    trevor40 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    407
    Quote Originally Posted by June7 View Post
    This works:

    oW.Worksheets("Sheet1").Range("M8") = "=IF(OR(Sheet1!$B$8 = " & Chr(34) & "SAT" & Chr(34) & ", Sheet1!$B$8 = " & Chr(34) & "SUN" & Chr(34) & "), 0, IF(Sheet1!$K$8 > 8, 8, Sheet1!$K$8))"
    Again thanks, of course things have changed and the office people have changed their formulas to incorporate the weekends and weekdays into one formula for each of - normal time, 1.5 and 2 times, they used to have separate code for each line on their worksheet and manually enter the data.

    They did this so that when the formulas (from my reference sheet) are "put" into the cells of the list they automatically set all list rows.

    so now i have to rethink the whole thing, I think that the simplest way is to use a preformatted excel sheet with all of thier formulas in it, and put my data from access into that. I have only ever used transferspreadsheet like the code below.

    Can you help me with going in the other direction and using an existing spreadsheet and import the data from access into it. I expect that once the link is esablished and the data placed in, the formatting will be the same?

    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Employee Time Report Output", gg, True
    Set Excel_Workbook = GetObject(gg)
    Set Excel_Application = Excel_Workbook.Parent
    Excel_Application.WindowState = xlMinimized
    Excel_Application.Visible = True
    Excel_Workbook.Windows(1).Visible = True
    Excel_Workbook.Worksheets(1).Name = "Time Report Output"
    Excel_Workbook.Worksheets("Time Report Output").Tab.ColorIndex = 37
    Set Current_Worksheet = Excel_Workbook.Worksheets("Time Report Output")
    Excel_Workbook.Worksheets("Time Report Output").Select
    Current_Worksheet.Cells.Select
    With Selection
    Current_Worksheet.Cells.HorizontalAlignment = xlRight
    Current_Worksheet.Cells.Font.Name = "Times New Roman"
    End With
    Current_Worksheet.PageSetup.Orientation = xlLandscape
    Current_Worksheet.Range("A1:P1").HorizontalAlignme nt = xlCenter
    Current_Worksheet.Range("A1:P1").VerticalAlignment = xlCenter
    Current_Worksheet.Range("A1:P1").Font.Bold = True
    Current_Worksheet.Range("A1:P1").Interior.ColorInd ex = 14
    Current_Worksheet.Range("A1:P1").Font.ColorIndex = 2
    ...
    ...

  15. #15
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Have you seen Ken Snell's site:
    http://www.accessmvp.com/kdsnell/EXCEL_MainPage.htm

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 41
    Last Post: 05-29-2015, 02:58 PM
  2. How to use this formula from Excel in Access?
    By jset818 in forum Queries
    Replies: 11
    Last Post: 10-16-2014, 03:32 PM
  3. Using an Excel Formula in Access
    By BEI77 in forum Access
    Replies: 6
    Last Post: 05-08-2014, 05:08 PM
  4. Excel Formula into Access Query
    By dr4ke in forum Queries
    Replies: 7
    Last Post: 06-25-2012, 06:46 AM
  5. Excel Formula Needed in Access
    By bmschaeffer in forum Queries
    Replies: 4
    Last Post: 01-18-2012, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums