PDA

View Full Version : Solved: create User Friendly Spreadsheet



Djblois
03-26-2007, 08:54 AM
A lot of individuals in my company are not that good with Excel, so to try to make things look good they add blank columns at the beginning to center things and blank rows. I am creating a macro that will fix up formating for them and for myself if they send me a spreadsheet like that. This is what I have so far:

Sub FormatTable()
Dim finalRowLast As Long
Dim finalHeader As Long
Dim cellObject As Range

Error00_00_01code
If Range("A1") = "" Then
End
End If

SpeedyPageSetup
TurnOffFeatures
With ActiveSheet
finalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
finalColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
finalRowLast = .Cells(Rows.Count, finalColumn).End(xlUp).Row
If finalRowLast > finalRow Then finalRow = finalRowLast
End With

Cells(2, 1).Resize(finalRow, finalColumn).Interior.ColorIndex = xlNone

For i = 3 To finalRow Step 2
Cells(i, 1).Resize(1, finalColumn).Interior.ColorIndex = 34
Next

Range("A1").Resize(1, finalColumn).Select
For Each cellObject In Selection
cellObject.Formula = WorksheetFunction.Proper(cellObject.Formula)
Next

ResizeAndFit
ColumnHeadingsA

End Sub

A few changes I want to make that I can't figure out how to do is:

I want it to find the first cell in the table (Eg: B1 or D4) then I want to delete all the rows and columns until it is A1
Next, I want it to delete any blank rows in between.This would only be used on Tables created by Users, no other types of spreadsheets

Zack Barresse
03-26-2007, 09:22 AM
Delete blank rows? Maybe something like this ...


Sub DeleteBlankRows()
Dim ws As Worksheet, LastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
LastRow = ws.Cells.Find(what:="*", after:=ws.Range("A1"), searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For i = LastRow To 1 Step -1
If WorksheetFunction.CountA(ws.Rows(i)) = 0 Then
ws.Rows(i).Delete
End If
Next i
End Sub

Djblois
03-26-2007, 09:49 AM
Thank you, I am about to test that but how do I get it in the A column if it isn't in the A column already?

Zack Barresse
03-26-2007, 09:57 AM
What do you mean "in the A column"?

lucas
03-26-2007, 10:03 AM
questions, questions, questions.......

Djblois
03-26-2007, 10:06 AM
if the first cell is B1, I would like to delete the A column, if it is C1, I would like to delete the A column and the B column

Bob Phillips
03-26-2007, 10:11 AM
Dim rng As Range

Set rng = Range("A1").SpecialCells(xlCellTypeConstants, 23)

If rng.Column > 1 Then
Columns(1).Resize(, rng.Column - 1).Delete
End If

If rng.Row > 1 Then
Rows(1).Resize(rng.Row - 1).Delete
End If

Djblois
03-26-2007, 10:56 AM
THis is what I have now and it works great except sometimes it is deleting the first column with data:


Dim finalRowLast As Long
Dim finalHeader As Long
Dim cellObject As Range
Dim rng As Range
Set firstUserForm = TableFormating

Error00_00_01code

SpeedyPageSetup
TurnOffFeatures

Set rng = Range("A1").SpecialCells(xlCellTypeConstants, 23)

If rng.Column > 1 Then
Columns(1).Resize(, rng.Column - 1).Delete
End If
If rng.Row > 1 Then
Rows(1).Resize(rng.Row - 1).Delete
End If

With ActiveSheet
finalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
finalColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
finalRowLast = .Cells(Rows.Count, finalColumn).End(xlUp).Row
If finalRowLast > finalRow Then finalRow = finalRowLast
End With

Cells(2, 1).Resize(finalRow, finalColumn).Interior.ColorIndex = xlNone

If TableFormating.YesBand Then
For i = 3 To finalRow Step 2
Cells(i, 1).Resize(1, finalColumn).Interior.ColorIndex = 34
Next
End If

Range("A1").Resize(1, finalColumn).Select
For Each cellObject In Selection
cellObject.Formula = WorksheetFunction.Proper(cellObject.Formula)
Next

ResizeAndFit

If TableFormating.HeaderYes Then
ColumnHeadingsA
End If
If TableFormating.YesTotals Then
TotalRowCommon
End If

End Sub

Bob Phillips
03-26-2007, 10:58 AM
Daniel,

Can you post an example workbook where it does that, as it worked in my tests.

Djblois
03-26-2007, 11:03 AM
ok

lucas
03-26-2007, 11:08 AM
Bob's code deletes rows 1 & 2
and
deletes Columns A & B
tested.

lucas
03-26-2007, 11:15 AM
correction....it was getting one of the columns...try this variation of Bob's code:
Dim rng As Range

Set rng = Range("A1").SpecialCells(xlCellTypeConstants, 23)

If rng.Column > 1 Then
Columns(1).Resize(, rng.Column - 2).Delete
End If

If rng.Row > 1 Then
Rows(1).Resize(rng.Row - 1).Delete
End If

Bob Phillips
03-26-2007, 12:06 PM
That might fail in other circumstances, this is more generic Dim rng As Range
Dim oArea As Range
Dim nFirstCol As Double
Dim nFirstRow As Double
Set rng = Range("A1").SpecialCells(xlCellTypeConstants, 23)
nFirstCol = 10 ^ 10
For Each oArea In rng.Areas
If oArea.Column < nFirstCol Then nFirstCol = oArea.Column
End If
Next oArea
If nFirstCol > 1 Then Columns(1).Resize(, nFirstCol - 1).Delete
End If
nFirstRow = 10 ^ 10
For Each oArea In rng.Areas
If oArea.Column < nFirstRow Then
nFirstRow = oArea.Row
End If
Next oArea
If rng.Row > 1 Then
Rows(1).Resize(nFirstRow - 1).Delete
End If

Fixed: Lucas

Djblois
03-26-2007, 12:13 PM
That works now