Josh - On the basis of what you added you've reinforced my first guess "you
are trying to copy the inside vertical from your single cell source, which
does not exist " and I'll add a second:
In the versions in which your code works I "guess" your source is NOT a
single cell whereas in XL2003, where it fails, source IS a single cell.
Effectively you are doing this
Dim SourceBorder As Border
Dim source as range
Set source = mySingleCell
Set SourceBorder = source.borders(xlInsideVertical)
But a single cell doesn't have an inside vertical border, so when you try
and copy to your multi column target it will fail.
Think you need to apply same check that "source" has inside borders as you
have with "target"
Not sure why you are even using the Borders object variable. Having done
your checks for Insides in both source and target, simply:
target.Borders(borderType).Weight = source.Borders(borderType).Weight
Afraid my OE has taken upon itself to remove all attachments from posts in
ng's deeming them unsafe, so I can't see your screenshot of those strange
constants!
Regards,
Peter T
"Josh Sale" <jsale@tril dot cod wrote in message
...
Peter,
Please see my replied in context:
Are you sure you are correctly applying your border properties, ie how
is
borderType defined in the calling procedure.
The calling procedure uses the Excel constants. For example:
CopyBorder xlInsideVertical, target, source
so they should be valid.
Also are you sure you are reading those constants correctly, AFAIK there
is
no value 11 for any of the LineStyles, in any version. In Object browser
search xllinestyle and examine all the corresponding constants My guess
is
you are trying to copy the inside vertical from your single cell source,
which does not exist.
XlInsideVertical = 11 !!
I agree neither 11 or 241 correspond to any of the defined LineStyle
constants. However, as you (hopefully the NG can handle attachments) can
see from the attached screen shot (Excel 2003) the LineStyle is 241 (I got
the 11 under Excel 97 ... where the code works fine!). I can only assume
its just a conincidence that xlInsideVertical resolves to the same value
as
I get under Excel 97.
You will need to loop properties for each border, unless all the
properties
for each border are the same and, you source from a single cell or
similar
size range, in which case you could do something like:
[d2:f6].Borders.LineStyle = [b2].Borders.LineStyle 'Weight & ColorIndex
If looping borders and applying same to each you could do:
bDoInsideVert = False
bDoInsideHoriz = False
For i = 7 To 12
If i = 11 And bDoInsideVert = False Then
ElseIf i = 12 And bDoInsideHoriz = False Then
Else
[d2:f6].Borders(i).LineStyle = [b2].Borders.LineStyle
[d2:f6].Borders(i).Weight = [b2].Borders(i).Weight
[d2:f6].Borders(i).ColorIndex = [b2].Borders(i).ColorIndex
End If
Next
The routine is always called with the Source range set to a single cell.
So
I don't think looping should be necessary.
To be ultra picky, you should change
borderType As Integer
to
borderType As Long
I suppose. However the range of all defined BorderIndex constants (5 to
12)
are easily contained in an integer. I think if this were the problem, the
code would fail 100% of the time. However, the caller of this routine
successively calls it (xlEdgeLeft, xlEdgeRight, etc) and all of those
calls
work. So far its only having trouble on xlInsideVertical.
Any other thoughts? Thanks.
josh
"Josh Sale" <jsale@tril dot cod wrote in message
...
I have code that copies border formatting from one cell to a range.
Its
been working fine. But I'm now getting a runtime error "Unable to set
the
Weight property of the Border class" trying to copy an xlInsideVertical
border. The same error occurs trying to copy the LineStyle property.
I know that a common problem with the xlInsideVertical border is that
it
can
only be applied to a multi-column range. My code checks for this
condition.
However in this case, the target range is B20:E20.
I went back and retested this code under XL97, XL2000, XL2002 and
XL2003
(all versions include all of the latest MS updates). It works under
XL97
but fails under all of the later releases.
If I manually open the workbook under XL97 and then examine the source
cell's border in a watch window it has a LineStyle of 11 and a Weight
of -4138 (this is the case where the copy works). If I repeat this
procedure under XL2003, LineStyle is 241 and Weight is 225!
I presume this difference is what's causing it to work under XL97 and
fail
under later versions.
I've looked at all of the LineStyle constants and neither 11 or 241 is
defined! I don't know what to make of that?
Here's the code in question:
Private Sub CopyBorder(borderType As Integer, Target As Range, source
As
Range)
Dim TargetBorder As Border
Dim SourceBorder As Border
If Not (borderType = xlInsideHorizontal And Target.Rows.Count < 2 Or
_
borderType = xlInsideVertical And Target.Columns.Count <
2)
Then
Set TargetBorder = Target.Borders(borderType)
Set SourceBorder = source.Borders(borderType)
TargetBorder.Weight = SourceBorder.Weight
TargetBorder.LineStyle = SourceBorder.LineStyle
TargetBorder.color = SourceBorder.color
TargetBorder.ColorIndex = SourceBorder.ColorIndex
End If
End Sub
Again, its the assignment of Weight and LineStyle that fail. The
assignment
of Color and ColorIndex work fine.
Any thoughts?
TIA,
josh