VBA Problem: Changing top border thickness if...

Nepumbra

New Member
Joined
Aug 15, 2014
Messages
5
Hello, I am pretty new to VBA and am trying to write something that will make the top border between B and G thin where the C column is not empty, and make the top border thick where the B column is not empty. If both C and B are not empty it should make the border thick. Here is what I did:

Sub Underline()

Dim rng As Range, C As Range, x As Range, B As Range, rng2 As Range, z As Range
Set C = Range("C2:C797")

For Each x In C
Set rng = Range("B" & x.Row & ":G" & x.Row)
Set B = Range("B" & x.Row)
If Not IsEmpty(B.Value) Then
rng.Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ElseIf Not IsEmpty(x.Value) Then
rng.Select
With Selection.Borders(x1EdgeTop)
.LineStyle = x1Continuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = x1Thin
End With
End If
Next
End Sub

The problem occurs in the second "With Selection.Borders(x1EdgeTop)". It gives me a run time error 1004, Application defined error. No idea why...
Any help would be much appreciated!
Thanks!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If Not IsEmpty(B.Value) Then
Unless the value in B is also an Object variable, VBA will ignore this line of code. The IsEmpty function only applies to Object Variables to test if they have been initialized. You could Use:
Code:
If IsEmpty(B)
because you have made B an object variable representing a range. But it will not test the value of B, only whether B is initialized as an object. I think what you are trying to do is:
Code:
If B.Value = "" Then
which will check to see if there is a value > blank in that cell.
 
Upvote 0
That should be:
Code:
If B.Value <> "" Then
 
Upvote 0
I made the changes, but I still get an error on the line:

ElseIf x.Value <> "" Then
rng.Select
With Selection.Borders(x1EdgeTop)
.LineStyle = x1Continuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = x1Thin
End With
End If

Any ideas? I can't figure it out.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

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