Solved

Error when attempting to ignore omitted cells error

Posted on 2014-09-12
4
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 48

Author Closing Comment

by:Dale Fye
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 48

Author Comment

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

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

636 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