返回列表 上一主題 發帖

[發問] 如何使用VBA開啟另外一個EXCEL檔案,但不啟動對方巨集

[發問] 如何使用VBA開啟另外一個EXCEL檔案,但不啟動對方巨集

請問一下,因為我想做一個 資料載入器的功能,我兩個資料夾都有VBA的程式
我現在想用B資料當BASE,載入至A檔案中計算
但因為B檔案開啟時會啟動對話巨集,所以我在A檔案中設定了關閉巨集,來停止B檔案的對話巨集
但也導致我A檔案巨集也被停止了,我該如何處理


ttt.zip (20.5 KB)
Sub test()
    Dim mybook As Workbook
    Dim mysh As Worksheet, dtsh As Worksheet
    Dim cell As Range
    Dim num As Long
    Dim secAutomation As MsoAutomationSecurity
   
    Set mybook = ThisWorkbook
    Set mysh = mybook.ActiveSheet

    mypath = mybook.Path & "\"
    dtfile = Dir(mypath & "*.xls")
    r = 2
    Do Until dtfile = ""
        If dtfile <> mybook.Name Then
            ck = ""
            For Each wb In Workbooks
                If wb.Name = dtfile Then ck = "Y"
            Next wb
  
   secAutomation = Application.AutomationSecurity
   Application.AutomationSecurity = msoAutomationSecurityForceDisable  '------關閉巨集

            If ck = "" Then Workbooks.Open mypath & dtfile
   Application.AutomationSecurity = secAutomation  '-----------------巨集啟用
            Set dtsh = Workbooks(dtfile).ActiveSheet

Workbooks(dtfile).Sheets(1).Range("a11:x850").Copy         '複製 B.xls Sheet1的 a1到c3

Windows(mybook.Name).Activate
Sheets(1).Select
Sheets(1).Range("a11").Activate
ActiveSheet.Paste



                    r = r + 1
                End If
         If dtfile <> mybook.Name Then
            Workbooks(dtfile).Close savechanges:=False
         End If
        dtfile = Dir
    Loop
End Sub

回復 1# lichang
我是這樣做的   參考參考
dim wb  as workbooks
dim wb1 as workbooks
Set wb = ThisWorkbooks
Application.EnableEvents = False     '使打開wb1時   巨集失效
Set wb1 = Workbooks.Open(FileName)
With wb1
             .Sheets(SHname).Cells.Copy wb.Sheets("Temp").Cells(1, 1)
             Application.CutCopyMode = False
             .Close False
End With
Set wb1 = Nothing
Application.EnableEvents = True   '恢復巨集效力

TOP

謝謝您的提供,我會測試看看!!謝謝!!

TOP

回復 2# yangjie


    你好,我測試過您的方式,確實可以避開巨集,很不錯!!

    那請問一下如果我有dde或RTD 外部連結資料,有辦法避開直接不連結嗎??

TOP

回復 4# lichang
對不起
dde或RTD 外部連結資料   我不孰 可請教版大

TOP

回復 5# yangjie

OK,感謝您熱心回答,謝謝!!

TOP

        靜思自在 : 心中常存善解、包容、感思、知足、惜福。
返回列表 上一主題