VBA使用Application.run +禁用只读功能

时间:2016-09-20 15:32:06

标签: vba

我有一个Mainworkbook,作为其他生产工作簿的文件管理器。 Mainworkbook通过启动以下代码连接到位于每个生产文件中的标准模块:

'Open File if not Read Only
Set wbkReadTest = Workbooks.Open(DeskWorkfileSearchPath & WorkfileSearch, notify:=False)

If Not wbkReadTest.ReadOnly Then

     'Run MainMacro with ErrorStage set to Default 0
     Application.Run "'" & WorkfileSearch & "'" & "!MainMacro",     
     ElsenecoParameters, ObjectNumber, ObjectType, InputMatrix, MaxInputsNumber, 
     WorkfileName, Desk, PartSearch

然后在每个生产工作簿中,有一些宏需要检索输入数据。该数据包含在基本工作簿中,可以只读而不会出现问题。我有大约40个生产工作文件,大约500个宏和大约500-1000个workbooks.open命令在所有这些组合中。

我的问题如下:当Mainworkbook打开一个生产文件,然后打开一个数据文件(假设它被其他人打开),我得到了只读弹出窗口,从而阻止了整个生产循环。现在,如果我直接打开生产文件并运行宏,数据文件将以只读方式打开而不通知(这正是我想要的)。

两种方法之间的唯一区别似乎是上面列出的application.run代码。以下代码在任何地方都已经完好无损:

Application.ScreenUpdating = False
Application.DisplayAlerts = False

如何解决此问题?除非另有说明,否则我基本上默认设置为只读。

添加了注释:

中的完整代码
Sub Construct() 'OK PLL

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

'Sections:
'1: Launch Initial Process
'2: Update Summary CAD Data
'3: Update Summary USD Data
'4: Update Forecast CAD

'''''''''''''''''''''''''''
'1: Launch Initial Process'
'''''''''''''''''''''''''''

Call MatchingCP
Call MatchingSummary

Call SummaryCAD
Call SummaryUSD

Call ForecastCAD

Sheets("CP").Select

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True

End Sub

Sub SummaryCAD() 'OK PLL

''''''''''''''''''''''''''''
'2: Update Summary CAD Data'
''''''''''''''''''''''''''''

'Condition if Base Currency <> Report Currency
If BaseCurrency <> ReportCurrency Then

        'Process to retrieve Currency Specific Data:

            'Retrieve Current Spot Rate
            MDSPOT_File = Sheets("CP").Range("bFile").Value & Format(Sheets("CP").Range("LBD").Value, "yyyymmdd") & ".xls"

            'Process if file is not found
            FailNumber = 0

            Do Until Dir(Sheets("CP").Range("bPath").Value & MDSPOT_File) <> ""
                FailNumber = FailNumber + 1
                MDSPOT_File = Sheets("CP").Range("bFile").Value & Format(Sheets("CP").Range("LBD").Value - FailNumber, "yyyymmdd") & ".xls"
            Loop

            'Open MDSPOT Workbook
            Workbooks.Open Sheets("CP").Range("bPath").Value & MDSPOT_File
            Sheets("INTL").Select

            'Retrieve Exchange Rate
            Workbooks(MDSPOT_File).Activate
            MDSPOTBaseTargetRow = Sheets("INTL").Cells.Find(What:=BaseCurrency, LookIn:=xlFormulas, lookat:=xlWhole).Row
            ExchangeValue = Sheets("INTL").Cells(MDSPOTBaseTargetRow, 2).Value

            'Close MD SPOT
            Workbooks(MDSPOT_File).Close SaveChanges:=False



            'Retrieve Previous Spot Rate
            MDSPOT_File = Sheets("CP").Range("bFile").Value & Format(Sheets("CP").Range("LBDPrevious").Value, "yyyymmdd") & ".xls"

            'Process if file is not found
            FailNumber = 0

            Do Until Dir(Sheets("CP").Range("bPath").Value & MDSPOT_File) <> ""
                FailNumber = FailNumber + 1
                MDSPOT_File = Sheets("CP").Range("bFile").Value & Format(Sheets("CP").Range("LBDPrevious").Value - FailNumber, "yyyymmdd") & ".xls"
            Loop

            'Open MDSPOT Workbook
            Workbooks.Open Sheets("CP").Range("bPath").Value & MDSPOT_File
            Sheets("INTL").Select

            'Retrieve Exchange Rate
            Workbooks(MDSPOT_File).Activate
            MDSPOTBaseTargetRow = Sheets("INTL").Cells.Find(What:=BaseCurrency, LookIn:=xlFormulas, lookat:=xlWhole).Row
            ExchangeValuePrevious = Sheets("INTL").Cells(MDSPOTBaseTargetRow, 2).Value

            'Close MD SPOT
            Workbooks(MDSPOT_File).Close SaveChanges:=False

End Sub

0 个答案:

没有答案