Solved

Excel97 - Error Trapping

Posted on 2002-07-30
3
263 Views
Last Modified: 2008-03-04
I have recorded a macro that works well for me.
The macro formats cells to my requirements.
There is however a problem when I try to macro format just a single cell.
The VBA errors because I do not have in my selection either
With Selection.Borders(xlInsideVertical) or
With Selection.Borders(xlInsideHorizontal).

In other words a selected single cell does not contain
xlInsideVertical or xlInsideHorizontal lines.

I also get the problem (if I highlight a single row of cells) with
     With Selection.Borders(xlInsideHorizontal)
as there are no inside horizontal lines.

I also get the problem (if I highlight a single column of cells) with
With Selection.Borders(xlInsideVertical)
as there are no inside vertical lines.

In order to avoid these error messages I need to be able to test if the selection is a single cell then ignore the code:-

     With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 2
         End With
   
     With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
           .Weight = xlThin
          .ColorIndex = 2
         End With
   
If the selection is a single column of cells ignore:-  
      With Selection.Borders(xlInsideVertical)
         .LineStyle = xlContinuous
         .Weight = xlThin
         .ColorIndex = 2
         End With
   
If the selection is a single row of cells ignore :-
     With Selection.Borders(xlInsideHorizontal)
           .LineStyle = xlContinuous
          .Weight = xlThin
         .ColorIndex = 2
        End With

I would be grateful if someone could show me how to do this please.
Thanks
0
Comment
Question by:BlueFin
3 Comments
 
LVL 3

Accepted Solution

by:
Elmo_ earned 100 total points
ID: 7187835
Bluefin,

If you want a really simple, quick and easy solution to this:

Type:

On Error Resume Next

After the Variable declarations in the Procedure but before the code.

This will skip to the next line if an error is encountered, thereby not interrupting your macro and still formating the cells as required.

Cheers,

Ed.
0
 
LVL 7

Expert Comment

by:ildc
ID: 7187995
or you could do it like this :

if selection.rows.count > 1 then
With Selection.Borders(xlInsideHorizontal)
           .LineStyle = xlContinuous
          .Weight = xlThin
         .ColorIndex = 2
        End With
   
if selection.columns.count >1 then
    With Selection.Borders(xlInsideVertical)
       .LineStyle = xlContinuous
       .Weight = xlThin
       .ColorIndex = 2
        End With
   
   

endif
else
if selection.columns.count >1 then
    With Selection.Borders(xlInsideVertical)
       .LineStyle = xlContinuous
       .Weight = xlThin
       .ColorIndex = 2
        End With
   
   

endif

endif


regards
0
 

Author Comment

by:BlueFin
ID: 7190119
Thanks Elmo that worked fine.
Thanks ildc - I can see that your solution would work fine too.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

807 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