Solved

Excel97 - Error Trapping

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export Outlook 'ghost contacts' 11 40
VPN speed vs Internet Bandwidth 3 40
Need adjust data counting 17 17
Find the duplicates in 32 26
This article will show you how to use shortcut menus in the Access run-time environment.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

730 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