Solved

Error when attempting to ignore omitted cells error

Posted on 2014-09-12
4
319 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

706 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now