VBA code - Borders for Table

Spaztic

New Member
Joined
Jul 27, 2023
Messages
48
Office Version
  1. 365
Platform
  1. Windows
This seems like it would be straight-forward but I'm struggling. I have a program that makes several tables (and that works). What I can't seem to do is apply 'All Borders' to each table it creates. The code that I added that doesn't work is:

Range(.ListObjects(i)).Select
With Selection.Borders
.LineStyle = x1Continuous
.Weight = x1Thin
.ColorIndex = x1Automatic
End With


my attempted code that doesn't work. (I've tried many different things with no success).

Any help would be appreciated!

VBA Code:
Sub CreateTables()
    Dim lr As Long, i As Integer
    Dim cll As Range, rng As Range

With ActiveSheet
    lr = .Cells(Rows.Count, "B").End(xlUp).Row  '<-- changed to column B
    Set rng = .Range("B1:B" & lr)
   
    For Each cll In rng
        If cll.Value = "Check Item" Then 'find header row
            i = i + 1
            .ListObjects.Add(xlSrcRange, .Range(cll, cll.End(xlDown).Offset(, 9)), , xlYes).Name = cll.Offset(-1, -1).Value
            .ListObjects(i).TableStyle = ""            'Use blank format for table
            .ListObjects(i).DataBodyRange.AutoFilter   'Remove dropdown arrows for each column in table

Range(.ListObjects(i)).Select
With Selection.Borders
                .LineStyle = x1Continuous
                .Weight = x1Thin
                .ColorIndex = x1Automatic
End With

        End If
    Next cll
End With
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
use xl (that's a lower case L) instead of x1 (that's the number one)

If you were using Option Explicit
Excel would have pointed out something wasn't right
 
Upvote 0
Solution
Thanks! That worked...and a simple solution. Just to be thorough...I changed that portion of the code to make it simpler.
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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