简体   繁体   中英

EXCEL 2010 VBA Macro Stopping

I have created an EXCEL 2010 workbook (eg, workbook1) which has a macro that (1) opens up another workbook (eg, workbook2), (2) extract data from workbook2 and puts information into an array of the workbook1 VBA code, (3) closes workbook2, and then continues running the macros in workbook1.

Macro works if (1) you run macro from VBA or (2) you run macro from developer/macros selection.

Macro opens workbook2 and then macro stops if I run macro through shortcut (SHIFT_CNTL_U)?

If shortcut just calls macro, any thoughts on why it would stop?

Simple Example of VBA code below (macro stops succesfully executing Workbooks.Open command:

Sub TESTER()
Dim DataMatrix(5) as integer, i as integer
Dim Analysis(5) as integer
Dim ws As Worksheet, wb As Workbook
'
    Set wb = Workbooks.Open("c:\workbook2.xlsx")
    Sheets("Data").select
    Range("A5").select
    For i = 0 to 4
       DataMatrix(i) = activecell.offset(0,i).value
    Next i
    wb.close
'    
    For i = 0 to 4
       Analysis(i) = DataMatrix(i) * 2
    Next i
'
end sub

I believe the problem is using the Shift key in the shortcut. I was able to replicate the behaviour in Excel 2016. When I removed the Shift key from the shortcut, it worked fine. See the link referred to by GMalc above. The issue is that when you hold down the Shift key when opening any Excel workbook, it prevents macros running. This behaviour apparently carries over to invoking macros via hotkey.

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