Selected Answer
Leopard
Sorry but this makes little sense to me - I have only a short time to review it and you haven't commented your code to give clues to readers.
You use the worksheet function INDEX in this line:
a = Application.Index(.items, 0, 0)
which would return just a single item but that function expects a value of 1 or more for the row and column numbers - that's why you get the error.
If you change it to:
a = Application.Index(.items, 1, 1)
would return the string "ITEMS" (the first entry in .Items) since the .items is a Method which applies to the scripting dictionary and produces an array of all dictionary items (see Microsoft gudance here: Microsoft Dictionary object).
I don't think that's what you want. You could replace the line with:
a = .Items
which would combine the dictionaries and note that the last item "Totals..." has 7 items not 5 like the rest.
Suggest you step through the code and see what a becomes after that step.
Unfortunately it's an array of arrays so you can't use the UBound function in that case. You might need to set through the array (of arrays) to extract values.
Revision #1, 05 November 2026
Using my suggestion above creates an array of arrays so the code following that doesn't work (crerting a subscript error)
Accordingly in the attached file I've changed the end of the procedure as follows (changes in bold):
Next
.Add Chr(2), [{"TOTAL", "","" ,"" ,"=sum(r2c:r[-1]c)"}]
'a = Application.Index(.items, 0, 0)
a = .Items
End With
'With Sheets("stock").[k1].Resize(UBound(a, 1), UBound(a(2), 1))
' clear whatever is around k1 (bigger or smaller than results)
With Sheets("stock").[k1]
.CurrentRegion.Clear
End With
With Sheets("stock").[k1].Resize(UBound(a, 1) + 1, UBound(a(2), 1))
' show the area
.Borders.LineStyle = xlContinuous
'.Value = a
' loop the array of arrays to populate values
For p = 1 To UBound(a, 1) + 1
For n = 1 To UBound(a(2), 1)
.Cells(p, n) = a(p - 1)(n)
Next n
Next p
With Union(.Rows(1), .Cells(.Rows.Count, 1))
.Font.Bold = True
.Font.Color = vbWhite
.Interior.Color = 15189684
End With
.HorizontalAlignment = xlCenter
' resize columns
.Columns.AutoFit
End With
End Sub
Click the button "RT" and you should get the right result (with all text visible via the autofitting of columns).