Solved

Error when attempting to ignore omitted cells error

Posted on 2014-09-12
4
323 Views
Last Modified: 2014-09-13
I'm working on some code to export data from Access to Excel (via Access VBA) and then add some formulas to the worksheet.  The export process is working great.  

I then insert a formula over a range and format the numbers in that range, this also works great.  

However, since my formula is in the cell 2 columns over from the last of the columns that is being summed, I'm getting the green warning triangle with the omitted cells warning.  My current code is shown below.  However the rng.Errors line raises an error:

1004 Application-defined or object-defined error

and then kicks me out of excel.  Strangely, this also sets the visible property of the Excel object to False so I cannot see it in the quick launch bar, but it is visible in Task Manager.

Anybody have any ideas why this syntax is not working?
Public Sub Sum_Rows_and_Columns(StartCol As Integer)

    Dim lngRowCount As Long
    Dim lngColCount As Long
    Dim strFormula As String
   
    On Error GoTo ProcError
    
    Set xl = GetObject(, "Excel.application")
    Set wbk = xl.activeworkbook
    Set sht = wbk.activesheet
    
    lngRowCount = sht.usedrange.rows.Count
    lngColCount = sht.usedrange.columns.Count
    
    Set rng = sht.Range(sht.cells(2, lngColCount + 2), sht.cells(lngRowCount, lngColCount + 2))
    strFormula = "=Sum(RC[" & StartCol - (lngColCount + 2) & "]:RC[-2]"
    rng.Formula = strFormula
    rng.NumberFormat = "#,###.00"
    rng.Errors(5).Ignore = True     '5=xlOmittedCells

    sht.columns(lngColCount + 2).Entirecolumn.autofit
    
ProcExit:
    On Error Resume Next
    Set sht = Nothing
    Set wbk = Nothing
    Set xl = Nothing
    Exit Sub
    
ProcError:
    MsgBox "Sum_Rows_And_Columns" & vbCrLf & Err.Number & vbCrLf & Err.Description
    Debug.Print "Sum_Rows_And_Columns", Err.Number, Err.Description
    Resume ProcExit
    
End Sub

Open in new window

0
Comment
Question by:Dale Fye (Access MVP)
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40320382
I think you can turn off that error checking for the entire Excel instance by adding this command after your wbk and sht assignments:
xl.ErrorCheckingOptions.OmittedCells = False

Then remove the command at line 20 in your code (rng.Errors(5).Ignore = True)

Regards,
-Glenn

PS  I ran this Access VBA code on a dummy Excel workbook and never did see the "Omit Adjacent Cells" error flag appear even though it was turned on. Wierd.
0
 
LVL 47

Author Closing Comment

by:Dale Fye (Access MVP)
ID: 40321024
Glenn,

Worked like a charm.

Any idea why the other method did not work?  Based on the help file, I would have thought that method would work just fine.

Any chance you already have those warnings turned off in the Excel file you tested it on?
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40321069
Dale,

Maybe I didn't set up a data table close enough to your example, but when I turn all the error flagging ON, the only time I saw the error was when omitting cells in a vertical (column-based) function (like SUM).  When I was adding across columns, I didn't see the error.

Admittedly, I've never used the Errors(x).Ignore method in VBA, so I can't say what, if anything, is an issue with that statement.  (Microsoft reference: http://msdn.microsoft.com/en-us/library/office/ff195953(v=office.14).aspx)

-Glenn
0
 
LVL 47

Author Comment

by:Dale Fye (Access MVP)
ID: 40321079
I was getting the warning flags when i summed columns (not in the example)  and rows,
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question