On Oct 26, 1:53*pm, "Rick Rothstein"
<rick.newsNO.S...@NO.SPAMverizon.net> wrote:
> Dim CW As Double, Source As Range
> Set Source = Range("E5")
> CW = Source.ColumnWidth
> Source.Columns.AutoFit
> If Source.ColumnWidth < CW Then Source.ColumnWidth = CW
Now that it's finally dawned on me... I'm using that approach for all
columns.
Takes about 5 seconds for 375 rows x 10 columns.
The agenda is to have a "ScratchPad.xls" handy for when I need to run
a database query, paste the results into Excel, and email the .XLS to
somebody.
The code in question beautifies the sheet - bolding the header row,
expanding columns to fit, and right/left/center justifying columns
depending on data type.
Seems to work well enough, although it could be faster.
Here's the whole enchilada. I have an accellerator key "M"
programmed to execute "Beautify", so I just select the upper-left cell
of the sheet, paste the entire contents of whatever query I'm
running , do a Ctl+M, and I'm good to go. Thanks for the help.
-----------------------------------------------------------------------------------------------
Sub Beautify()
SetFont
ExpandCols
FixHeader
Cells(1, 1).Select
End Sub
Sub ExpandCols()
Dim curCell As Range
Dim testCell As Range
Dim lastRow As Long
Dim lastCol As Long
Dim lastCell As Long
Dim R As Long
Dim C As Long
Dim curWid As Double
Const incWid As Double = 1
Const maxWid As Long = 50
Const slopFactor As Long = 2
If WorksheetFunction.CountA(Cells) > 0 Then
' ---------------------------------------------
' Turn screen updating off to speed things up
Application.ScreenUpdating = False
' ---------------------------------------------
' - Determine last column/row
' - Set a pointer to a cell just beyond our last populated cell
lastCol = Cells.Find(What:="*", After:=[A1],
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Set testCell = Cells(lastRow + 1, lastCol + 1)
' ---------------------------------------------
' Make each column big enough to render largest text,
' but exclude headers from this operation
For R = 2 To lastRow
For C = 1 To lastCol
Set curCell = Cells(R, C)
curWid = curCell.ColumnWidth
curCell.Copy testCell
With testCell
.WrapText = False
.Columns.AutoFit
If .ColumnWidth > curWid Then
Columns(C).ColumnWidth = .ColumnWidth
End If
End With
Next C
Next R
testCell.Value = Null
' ---------------------------------------------
' - Make header row alignments same as detail row alignments
' except for dates where we force alignment to center
For C = 1 To lastCol
If IsDate(Cells(2, C)) Then
Columns(C).HorizontalAlignment = xlCenter
Else
Cells(1, C).HorizontalAlignment = Cells(2,
C).HorizontalAlignment
End If
Cells(1, C).Interior.ColorIndex = 15
Next C
' ---------------------------------------------
' - Clean up pointers
' - Turn screen updating back on
Set curCell = Nothing
Set testCell = Nothing
Application.ScreenUpdating = True
End If
End Sub
Sub FixHeader()
Rows("1:1").Select
With Selection
With .Font
.Name = "Arial Narrow"
.Size = 10
.Bold = True
End With
.Rows.AutoFit
.WrapText = True
End With
End Sub
Sub SetFont()
Cells.Select
With Selection.Font
.Name = "Courier New"
.Size = 10
End With
Selection.RowHeight = 12
Cells(1, 1).Select
End Sub
-----------------------------------------------------------------------------------------------
|