Hello All,
I have created a Macro that Loops through every selection in a Report Filter, copies the data to a new sheet, visually formats the data, and then names the sheet based on the value of K1 (which contains a formula that truncates the name in A1 to 30 charatcers or less).
It works near perfectly. However, if the sheet name already exists (there are similar truncated names in K1), it returns an error saying that the sheet name already exists. I want the macro to automatically add a "1" at the end of the sheet if the sheet name already exists. Here is my (and others) code; I apologize for the messiness...I am new to the game.
Sub CreateUsageReports()
'Selects Correct Sheet Before Starting'
Sheets("Lookup to Ledger").Select
Range("A1").Select
'Turn ScreenUpdating OFF'
Application.ScreenUpdating = False
'Store Sheet with the PivotTable'
Piv_Sht = ActiveSheet.Name
'Loop through every PivotItem in the PageField (Filter) of the PivotTable'
For Each PivotItem In ActiveSheet.PivotTables(1).PageFields(1).PivotItems
'Select the PivotItem'
ActiveSheet.PivotTables(1).PageFields(1).CurrentPage = PivotItem.Value
'Creates a Usage Report and Formats for e-Mail'
Cells.Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("A:A").EntireColumn.AutoFit
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlToLeft
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlToLeft
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Range("A1:D1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Selection.Style = "Note"
Range("A3:D3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Style = "60% - Accent1"
Range("D4").Select
Selection.Style = "Currency"
Range("C4").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("B4").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A4").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A:A,C:C,D:D").Select
Range("D2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
Range("A:A,C:C,D:D").Select
Range("D2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("B:B").EntireColumn.AutoFit
'Creates Temproary Code to Name Sheet'
Range("K1").Select
ActiveCell.FormulaR1C1 = "=IF(LEN(R1C1)>30, LEFT(R1C1,30),R1C1) & RANDBETWEEN(1,9)"
'Renames Sheet'
ActiveSheet.Name = [K1]
'Deletes Temporary Code Used to Format Sheet Name'
Range("K1").Select
Selection.Delete Shift:=xlToLeft
'Return to Sheet with the PivotTable'
Sheets(Piv_Sht).Select
Next
'Turn ScreenUpdating ON'
Range("B1").Select
Application.ScreenUpdating = True
End Sub
Essentially, if the following code fails, I want it to name the Sheet based on the value in [K2], which contains the formula that truncates the name with a "1" affixed at the end.
'Renames Sheet'
ActiveSheet.Name = [K1]
Bookmarks