Solved

Error when attempting to ignore omitted cells error

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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 on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

911 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

20 Experts available now in Live!

Get 1:1 Help Now