简体   繁体   中英

VBA Runtime Error going from Excel 2003 to 2016

I have a spreadsheet that was built with Macros in Excel 2003, which we no longer have available to us. I'm trying to run it in Excel 2016, and am trying to work through the macro errors that are appearing.

I'm somewhat capable with VBA, but by no means an expert, and as I didn't write these macros, I'm starting from scratch.

I get a Run-time error '1004': Application-defined or object-defined error on the .Group.Select line in the below code.

'Group all shapes into one box
With .Range(Array(sPutName, sTriName, sBoxName(0), sBoxName(1), sBoxName(2), _
        sBoxName(3), sBoxName(4), sBoxName(5), sBoxName(6), sBoxName(7)))
    sPutGrouped = "put" + Trim(str(puts - iPutStart + 1))
    .Group.Select
    .name = sPutGrouped
End With

Any advice on where to start looking?

EDIT

So it appears to be related to the below code, which appears immediately before the above snippet.

' A work around (HACK) to allow grouping of all the shapes
'  basically fills the remaining array elements with last one
            While sBoxName(box) = "" And (box < 8)
                sBoxName(box) = sBoxName(box - 1)
                box = box + 1
            Wend

Essentially, the macro draws a number of boxes between 1 and 8. If it only draws 2, then sBoxName(2) to sBoxName(8) are set to the same as sBoxName(1). If I know how many boxes it is going to draw beforehand, and delete the reference to the duplicate boxes from the .Range Array, then everything works. However, I need this statement to work with any number of boxes between 1 and 8.

Make sure you are running the code against a worksheet with some boxes (check the With statement above the one you gave in your example, to see which sheet the code is running against).

Try halting the code at

With .Range(Array(sPutName, sTriName, sBoxName(0), sBoxName(1), sBoxName(2), _
        sBoxName(3), sBoxName(4), sBoxName(5), sBoxName(6), sBoxName(7)))

Then in the immediate window, type:

?.Range(Array(sPutName, sTriName, sBoxName(0), sBoxName(1), sBoxName(2), _
        sBoxName(3), sBoxName(4), sBoxName(5), sBoxName(6), sBoxName(7))).address

You should get a valid cell reference string, such as "A1:A3,D2:D3". My guess is that you won't... and a solution to the problem may appear.

I solved my issue by creating an array and using ReDim to append the name of each box to the end of the array as the box was created. This meant the array was always the correct length for the actual number of boxes created, and I could delete the second code snippet above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM