- 帖子
- 128
- 主題
- 62
- 精華
- 0
- 積分
- 176
- 點名
- 0
- 作業系統
- win8
- 軟體版本
- office2003
- 閱讀權限
- 20
- 註冊時間
- 2013-1-24
- 最後登錄
- 2026-9-14
|
[發問] 如何使用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 |
|