'Select method of worksheet class failed' mid loop

filido

New Member
Joined
Jun 7, 2019
Messages
21
Hello,

My code should filter each value and copy charts to different sheets. I looped this but it breaks mid loop. It works fine with 8 first values/sheets (should be 12 or so). I get error 1004: Select method of worksheet class failed. I highlighted the part in red when the error occurs.


Sub CreateCharts()


Dim wsDATA, wsPIA As Worksheet
Dim loData As ListObject
Dim erow As Long

Set wsDATA = Sheet2
Set wsPIA = Sheet4
Set loData = Sheet2.ListObjects("Table1")


wsDATA.Select
ActiveSheet.Columns("D").Select
Selection.Copy

wsDATA.Columns("T").Select
ActiveSheet.Paste
ActiveSheet.Range("$T$1:$T$100000").RemoveDuplicates Columns:=1, Header:=xlYes


Dim ArrayDictionaryofItems As Object, Items As Variant, i As Long, Item As Variant
Set ArrayDictionaryofItems = CreateObject("Scripting.Dictionary")


With ActiveSheet


'create list of unique items in column D that get filled into ArrayDictionaryofItems
Dim ui As Double

If Range("D3").Value <> "" Then
ui = 2
Items = Range(.Range("T2"), .Cells(Rows.Count, "T").End(xlUp))


'fills ArrayDictionaryofitems to the UBOUND (max) count of unique items in column T.

For i = 1 To UBound(Items, 1)
ArrayDictionaryofItems(Items(i, 1)) = 1
Next

Else

Item = Range("D2").Value
ui = 1
End If


'Filter multiple items if ui is set to equal 2 because D3 is blank
If ui = 2 Then

For i = 4 To UBound(Items, 1)
Sheets.Add after:=Sheets(i)
Next i

wsDATA.Select

Dim x As Double
x = 5

For Each Item In ArrayDictionaryofItems.keys
erow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
'autofilter on column D with this driver
loData.Range.AutoFilter Field:=4, Criteria1:=Item

wsPIA.Select
ActiveSheet.Shapes("Sales").CopyPicture
Sheets(x).Select
Columns("A").Select
ActiveSheet.Paste

wsPIA.Select
ActiveSheet.Shapes("Quantity").CopyPicture
Sheets(x).Select
Columns("U").Select
ActiveSheet.Paste

wsPIA.Select
ActiveSheet.Shapes("Customer").CopyPicture
Sheets(x).Select
Columns("AO").Select
ActiveSheet.Paste

wsDATA.Select

x = x + 1
Next Item
GoTo LINE99:
End If


'filter a single item in column since D3 is blank and there is only one item in column D to filter
If ui = 1 Then

Sheets.Add after:=ActiveSheet
wsDATA.Select

Item = Range("D2").Value
.UsedRange.AutoFilter Field:=4, Criteria1:=Item
End If

wsPIA.Select
ActiveSheet.Shapes("Sales").CopyPicture
Sheets(2).Select
Columns("A").Select
ActiveSheet.Paste

wsPIA.Select
ActiveSheet.Shapes("Quantity").CopyPicture
Sheets(2).Select
Columns("U").Select
ActiveSheet.Paste

wsPIA.Select
ActiveSheet.Shapes("Customer").CopyPicture
Sheets(2).Select
Columns("AO").Select
ActiveSheet.Paste


End With

LINE99:
With ActiveSheet
If .AutoFilterMode Then .UsedRange.AutoFilter
End With


End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I figured this out by myself.

For i = 4 To UBound(Items, 1)
Sheets.Add after:=Sheets(i)
Next i

should be

For i = 0 To UBound(Items, 1)
Sheets.Add after:=Sheets(i+4)
Next i

in the upper line the macro created i - 4 sheets (in this case i = 12 so it created 12 - 4 = 8 sheets) so x did not find the next sheet to paste after going through the first 8 sheets.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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