Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

mismatch error when try calculation stock

0

Hi
I would extraction data across sheets to show final sock
should match columns F for sheets(SUU,SUUU,BYUUU,BUMNHH) and columns B for STOCK sheet


in file so should specify theses sheets.
as to QTY columns G(SUU-SUUU+BYUUU-BUMNHH sheets),C (STOCK sheet)should be STOCK+SUU-SUUU+BYUUU-BUMNHH
as TOTAL amount columns I (SUU-BUMNHH sheets) ,E(STOCK sheet) should be =STOCK+SUU-BUMNHH
QTY column depends all of sheets , but TOTAL amount column I just depends on SUU,BUMNHH,stock sheets.

the result will be in columns K:O in stock sheets .
sometimes there are new items in STOCK sheet ,but not existed in SUU sheet , the same thing there are new items in SUU sheet ,but not existed in STOCK sheet. it's not necessarily all of brands should be existed in each sheet.
if there is just brand in STOCK sheet then will brings price from column D
there is empty row each invoice to bottom

 show mismatch error in this line

a = Application.Index(.items, 0, 0)

any help to fix it,please?

Answer
Discuss

Answers

0
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).

Discuss

Discussion

Hi John again
I edited file ,I realized to not file what I work on.
about this 
a = Application.Index(.items, 1, 1)

show same error.
about this 
a = .Items

show subscript out of range 
With Sheets("stock").[k1].Resize(UBound(a, 1), UBound(a, 2))

anyway thanks for your try.
leopard (rep: 102) Nov 5, '26 at 2:38 am
Leopard- the the revision to my Answer and new file.
John_Ru (rep: 6872) Nov 5, '26 at 6:43 am
Many thanks John
leopard (rep: 102) Nov 5, '26 at 9:07 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login