Solved

Excel97 - Error Trapping

Posted on 2002-07-30
3
260 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Perhaps more familiar to developers who primarily use VBScript than to developers who tend to work only with Microsoft Office and Visual Basic for Applications (VBA), the Dictionary is a powerful and versatile class, and is useful …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

744 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