Solved

Excel97 - Error Trapping

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Outlook Free & Paid Tools
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 …
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

729 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