Forum

Creating Borders, x...
 
Notifications
Clear all

Creating Borders, xlMedium not working

8 Posts
3 Users
0 Reactions
482 Views
(@jomili)
Posts: 9
Active Member
Topic starter
 

I created the below macro today.  Going through it, if I choose xlThin or xlThick for my thickness it works fine, if I choose xlMedium it gives an "Unable to set the Weight property of the Border Class", and I can't figure out why.  The macro lives in my PERSONAL, and I've been testing it on a blank sheet, so no example workbook is attached.  Any help would be appreciated.

 

Sub BorderChooser()
'in Formatting Tools
Dim Area As String
Dim Style As String
Dim Thickness As String

Area = InputBox("All Choices work with your Entire Selection." & vbCrLf & vbCrLf & _
            "Typing A creates a Border around it all." & vbCrLf & _
            "    C  creates a Border around each Column." & vbCrLf & _
            "    R  creates a Border around each Row." & vbCrLf & _
            "    U  creates top and bottom borders for each row.", "How are we bordering?")
    If StrPtr(Area) = 0 Then Exit Sub   'User Canceled
Style = InputBox("How Thick do you want your lines?" & vbCrLf & _
                "Enter T for Thick, " & vbCrLf & _
                "S for Thin(Skinny), or" & vbCrLf & _
                "M for Medium.", "Choose your Line Weight")
    If StrPtr(Style) = 0 Then Exit Sub   'User Canceled
    Select Case UCase(Style)
        Case "T"
        Thickness = xlThick
        Case "S"
        Thickness = xlThin
        Case "M"
        Thickness = xlMedium
        Case Else
        Exit Sub
    End Select
    Select Case UCase(Area)
        Case "A"
        Selection.BorderAround Weight:=Thickness
        Case "C"
        With Selection
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideVertical).Weight = Thickness
        .BorderAround Weight:=Thickness
        End With
        Case "R"
        With Selection
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).Weight = Thickness
        .BorderAround Weight:=Thickness
        End With
        Case "U"
        With Selection
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).Weight = Thickness
        End With
        Case Else
        Exit Sub
    End Select
End Sub

 
Posted : 03/01/2020 1:52 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Jomili,

The variable Thickness is defined as a string but it should be a numeric e.g. Long

https://docs.microsoft.com/en-us/office/vba/api/excel.xlborderweight

Regards

Phil

 
Posted : 03/01/2020 8:30 pm
(@jomili)
Posts: 9
Active Member
Topic starter
 

OMG, I don't think I ever would have considered it numeric.  But you're absolutely right; changing it to a LONG makes all the difference.  Thank you!

 
Posted : 04/01/2020 9:47 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

🙂 No worries.  I think all (?) the constants in VBA are numeric values.  

 
Posted : 05/01/2020 1:17 am
(@debaser)
Posts: 836
Member Moderator
 

They are - Enums can only be whole numbers.

 
Posted : 07/01/2020 8:15 am
(@jomili)
Posts: 9
Active Member
Topic starter
 

That raises a question; in my original macro, where I dimmed Thickness As a String, xlThin or xlThick worked fine.  Why didn't they error, why was it only xlMedium?

 
Posted : 07/01/2020 12:03 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

VBA allows implicit conversion - converting one data type to another - and it handles the type conversion.  So when you assigned a number to a string, VBA just dealt with it.

When it came to assigning that string value to the Borders Weight, VBA seems to have handled the conversion back from a string to a number just fine for  xlThin (2) and xlThick (4).  I'm concluding that is because they are positive numbers.

However xlMedium has a value of -4138 and it seems VBA hasn't converted this back from a string to a number.  I don't know why this would be, I don't know how the underlying type conversion works.

If you used ActiveCell.Borders(xlInsideHorizontal).Weight = CLng(Thickness) then it would have worked as you would be explicitly converting the string to a long.

Cheers

Phil

 
Posted : 07/01/2020 7:41 pm
(@jomili)
Posts: 9
Active Member
Topic starter
 

Thanks Phillip.  I also thought that maybe the negative on xlMedium was throwing it off, but didn't understand why.  Thanks for the CLng trick, I can see that could be useful if I'd declared it as a string and needed it as a string for one use, but as a LONG for another. 

 
Posted : 08/01/2020 10:11 am
Share: