PDA

View Full Version : Formatting sheets with changing titles/numbers in a macro



AdrianK
02-25-2008, 09:59 AM
Hi there,

I am using the below code in a macro to extract unique records from a database extract onto a new sheet, which works fine, however I am encountering problems where each sheet then needs to have individual formatting performed once this function has run (subtotals, headers, print areas etc.) however, as the names and number of sheets is different each time the macro runs I am finding this difficult, and getting a lot of errors.

Is there a way that each sheet can be selected, have the formatting performed on it, and and then the next sheet selected until the end, without referring to the names or the number of sheets, as these are constantly changing?


Public Sub Unique_Record_Extract()

'extracts unique records from Column A and copies those records to a new worksheet

Dim My_Range As Range
Dim My_Cell As Variant
Dim sh_Original As Worksheet
Dim cl As Variant

'turn off interactive stuff to speed it up
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

'Rename the first sheet to generic name "Data"
Worksheets(1).Name = "Data"

'add a new worksheet to temporarily store unique record names
Set sh_Original = ActiveSheet

On Error Resume Next
Sheets("TEMPXXX").Delete
On Error GoTo 0
Worksheets.Add
ActiveSheet.Name = "TEMPXXX"

'copy all unique records from main sheet into temporary sheet
Worksheets("Data").Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Columns("A:A"), Unique:=True

'set up list of unique records stored on the temporary sheet
'start in cell A2 because the advanced filter will copy headers too.
Set My_Range = Range("A2:A" & Range("A65536").End(xlUp).Row)

'cycle through each unique entry in the list and filter the original sheet with that value
For Each My_Cell In My_Range
'create a new worksheet with unique record name (delete it first if it aleady exists)
On Error Resume Next
Sheets(My_Cell.Value).Delete 'delete if already exists
On Error GoTo 0
Worksheets.Add 'add new sheet which subsequently becomes the active sheet
ActiveSheet.Name = My_Cell.Value ' be careful here of worksheet names > 31 characters

'filter Original sheet
sh_Original.UsedRange.AutoFilter Field:=1, Criteria1:=My_Cell.Value

'copied filter list to the activesheet (which is the sheet just recently added)
sh_Original.Cells.SpecialCells(xlVisible).Copy Destination:=Range("A1")

'autofit the columns to make it look pretty
Columns.AutoFit

'remove blank cells
Range("A1").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Clear
Range("A1").Select

Next

'tidy up!
Worksheets("TEMPXXX").Delete
sh_Original.AutoFilterMode = False
Set sh_Original = Nothing

Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

End Sub

Any help would be greatly appreciated.

Many thanks

AK

Bob Phillips
02-25-2008, 10:36 AM
Haven't looked at your code, but from your question



For Each sh In Activeworkbook.Worksheets

'process this sheet ALWAYS qualifying the ranges by sh.Range ....
Next sh

mdmackillop
02-25-2008, 12:46 PM
Hi Adrian,
When you post code, select it and click the VBA button to format it as shown.
Regards
MD

mdmackillop
02-25-2008, 12:50 PM
Can you copy your data into a template sheet, preformatted with headers etc., similar to this question (http://www.vbaexpress.com/forum/showthread.php?t=17942)?

AdrianK
02-26-2008, 02:47 AM
Hi, thanks MD, will do in future.

The formatting code currently being run on each sheet is as below:


'Insert header
Sheets("Sheet Name").Select
Range("A1").Select
Workbooks.Open Filename:= _
"D:\My Documents\Reports\Disputed Invoices\Header template.xls"
Rows("1:1").Select
Selection.Copy
Windows("Disputed Invoices.xls").Activate
Rows("1:1").Select
Selection.Insert Shift:=xlDown

'Add Subtotals and format Sheet Name
Range("A1:J1").Select
ActiveCell.FormulaR1C1 = "DISPUTED INVOICES - SHEET NAME"
With ActiveSheet.Outline
.AutomaticStyles = True
.SummaryRow = xlBelow
.SummaryColumn = xlRight
End With
Selection.ApplyOutlineStyles
Range("A3").Select
ActiveWindow.freezepanes = True
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(7, 8), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$2"
.PrintTitleColumns = ""
End With
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.25)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0.25)
.FooterMargin = Application.InchesToPoints(0.25)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintErrors = xlPrintErrorsDisplayed
End With


The formatted version comes out as attached, some of the formatting can be done before the records are extracted, so that isn't a problem.

Thanks again.

AK

AdrianK
02-26-2008, 04:28 AM
Hi,

Thanks for all your help, I have solved the problem by putting the formating in the unique record extract code before it ticks round and moves on to the next sheet.

Thanks again for the suggestions.

AK