format borders of cell with VBA

white_flag

Active Member
Joined
Mar 17, 2010
Messages
331
Hello
I have the following code that will try to put some lines around cells that have values inside:

the code is like this but I received error 438 (Object doesn't support this property or method)
How can be this fixed?

error is starting from here:
Code:
With .Borders(xlEdgeTop)
Code:
Dim Cell As Range
With Sheets(strSheetName)
    For Each Cell In Sheets(strSheetName).Range("B1:J23")
         If Cell.Value > "" Then
               With .Borders(xlEdgeTop) 
                    .LineStyle = xlDot
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
                With .Borders(xlEdgeBottom)
                    .LineStyle = xlDot
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
                With .Borders(xlInsideHorizontal)
                    .LineStyle = xlDot
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
         End If
    Next
End With
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try

Code:
Dim Cell As Range
With Sheets(strSheetName)
    For Each Cell In .Range("B1:J23")
        With Cell
         If .Value <> "" Then
               With .Borders(xlEdgeTop)
                    .LineStyle = xlDot
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
                With .Borders(xlEdgeBottom)
                    .LineStyle = xlDot
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
                With .Borders(xlInsideHorizontal)
                    .LineStyle = xlDot
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
         End If
        End With
    Next
End With
 
Upvote 0
following your code

I have error 1004 (unable to set line property of the Border class) at line:

Code:
.LineStyle = xlDot
I do not understand why it is acting like this (I have an similar code in a different worksheet and it working ok)
on this one I just ad the if statement (if cell is not empty make the borders)..anyway
 
Upvote 0
Sorry, I don't know why you are getting that error. The macro recorder gives the same syntax for adding a dotted border.
 
Upvote 0
If I take this out

Code:
With .Borders(xlInsideHorizontal)
    .LineStyle = xlDot
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With

it is working but not the way I like ... I have to dig
 
Upvote 0

Forum statistics

Threads
1,215,562
Messages
6,125,546
Members
449,237
Latest member
Chase S

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top