Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel97 - Error Trapping

Posted on 2002-07-30
3
Medium Priority
?
269 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 400 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

721 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