VBA create and sort report

HakaWaka

New Member
Joined
Jun 22, 2011
Messages
3
Hi all,

Im not much of a VBA user but had recorded this macro. The purpose of this macro is to produce a report which uses pivot table data which gets put into a new report in a new worksheet, then once in a new worksheet the data gets sorted by column B first and then column A second, lastly after all that the worksheet gets renamed to "CodeReport".

What ive found was that once i run the macro the following error appears "Run-time error '9': Subscript out of range" and then when i click the Debug button the Red bold text below is highlighted in the VBA window. I had noticed that everytime i run the macro a new worksheet is created to output the pivot table report which is fine but the worksheet name keeps changing eg Sheet1....Sheet2.....Sheet3.....Sheet4 etc everytime i run the macro hence why i manually added the blue code below to keep the worksheet name constant as "Sheet2".

Any idea how to get around this issue?

THANK YOU :)


Sub Report()
'
' Report Macro
' Creates a report based on pivot table data, renames the new worksheet and sorts the report data
'
'
Range("L15").Select
Selection.ShowDetail = True
ActiveSheet.Name = "Sheet2"
Sheets("Sheet2").Select
ActiveWorkbook.Worksheets("Sheet2").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").ListObjects("Table1").Sort.SortFields.Add _
Key:=Range("Table1[Charge Code]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet2").ListObjects("Table1").Sort.SortFields.Add _
Key:=Range("Table1[Flag]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "CodeReport"
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top