Hi all,
For every work assignment in Excel I have a specific format for presenting data. I add outside borders to the headings and to each column in a selected area. Sometimes it is just one "table" (I don't actually create an excel table, I just add formatting to raw data), and other times I have multiple "tables" on a single sheet. Because I have to make many edits and resubmit the assignment multiple times, I am trying to create a code that will do this formatting automatically and regardless of whether it is one or several "tables" on a sheet.
My approach is to first find the (dynamic) range, then loop through every column in the selection and apply the formatting. So far I can get vertical borders on each column, but my bottom borders are at the "bottom" limits of the excel rows.
For example, I want
_______________________
|A1|B1|C1|D1|E1|F1|G1|H1|
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
________________________
instead of
_______________________
|A1|B1|C1|D1|E1|F1|G1|H1|
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
Here is the code I have so far (note that I have commented out previous code that didn't quite work):
Thanks for any help!
For every work assignment in Excel I have a specific format for presenting data. I add outside borders to the headings and to each column in a selected area. Sometimes it is just one "table" (I don't actually create an excel table, I just add formatting to raw data), and other times I have multiple "tables" on a single sheet. Because I have to make many edits and resubmit the assignment multiple times, I am trying to create a code that will do this formatting automatically and regardless of whether it is one or several "tables" on a sheet.
My approach is to first find the (dynamic) range, then loop through every column in the selection and apply the formatting. So far I can get vertical borders on each column, but my bottom borders are at the "bottom" limits of the excel rows.
For example, I want
_______________________
|A1|B1|C1|D1|E1|F1|G1|H1|
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
________________________
instead of
_______________________
|A1|B1|C1|D1|E1|F1|G1|H1|
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
Here is the code I have so far (note that I have commented out previous code that didn't quite work):
Code:
Sub COMPARISON_BUILD_TABLE_CODE()
'
' COMPARISON_BUILD_TABLE_CODE Macro
'
' LOOP THROUGH EACH COLUMN AND APPLY BORDERS
'Dim myRng As Range
Dim rng As Range
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
'Dim Last_Column As Integer
'Dim Last_Row As Long
'Refresh Used Range
'Set myRng = Worksheets("Top_10_DIAGS").UsedRange
'Select UsedRange
'myRng.Select
'Worksheets("Top_10_DIAGS").UsedRange.Select
'Last_Column = 0
'Last_Column = wsheet.Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
'Last_Row = wsheet.Cells.Find("*", [a1], , , xlByRows, xlPrevious).Row
'wsheet.Range(Cells(1, 1), Cells(Last_Row, Last_Column)).NumberFormat = "@" 'standard text formatting
Dim wsheet As Worksheet
Dim i As Long
Worksheets("Top_10_DIAGS").Activate
Set wsheet = ActiveSheet
'code to find range
Set rng1 = Cells.Find("*", , , , xlByRows, xlPrevious)
Set rng2 = Cells.Find("*", , , , xlByColumns, xlPrevious)
If Not rng1 Is Nothing Then
Set rng3 = Range(ActiveCell, Cells(rng1.Row, rng2.Column))
MsgBox "Range is " & rng3.Address(0, 0)
'if you need to actual select the range (which is rare in VBA)
Application.Goto rng3
Else
MsgBox "sheet is blank", vbCritical
End If
'line below was "For Each rng In Range(A1, Cells(Last_Row, Last_Column)).Columns"
'For Each rng In Range(a1, Cells(Last_Row, Last_Column)).Columns 'For Each rng In myRng.Columns
'For Each rng2 In rng3.Columns
For i = 1 To Selection.Columns.Count
Columns(i).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Next i
End Sub
Last edited: