2017-07-28 52 views
1

我想在Excel中創建一種用戶界面,並且需要找到一個簡單的解決方案將工作表從選定的工作簿複製到特定的工作簿。到目前爲止,我已經編寫了下面的代碼,但我不知道如何在代碼中引用選定的工作簿。 用戶需要將許多不同工作簿(具有不同名稱)的工作表(它稱爲Storyboard,名稱永遠不會更改)複製到使用VBA的工作簿。 現在可以選擇另一個工作簿,但是我不能在代碼中引用該工作簿。參考用filedialog在vba中選擇的工作簿

此外,用戶打算選擇的工作簿上有一些宏,如何在打開後禁用它們?

這是目前爲止的代碼;

Sub Storyboard_Ekle() 
    Dim DosyaSec As Office.FileDialog 

    Set DosyaSec = Application.FileDialog(msoFileDialogFilePicker) 

    With DosyaSec 

     .AllowMultiSelect = False 
     .Title = "Lütfen yeni eklenecek Storyboard dosyasini seçiniz." 
     .Filters.Clear 
     .Filters.Add "Excel Macro-Enabled Workbook", "*.xlsm" 
     .Filters.Add "Excel Workbook", "*.xlsx" 
     .Filters.Add "All Files", "*.*" 

     If .Show = True Then 
     YeniSB = .SelectedItems(1) 
     End If 

Dim YeniStoryBoard As Workbook 
Dim AnaDosya As Workbook 
Dim YeniStoryBoard_Sheet As Worksheet 
Dim AnaDosya_Sheet As Worksheet 

Application.ScreenUpdating = False 

Set AnaDosya = ThisWorkbook 
Application.EnableEvents = False 
Set YeniStoryBoard = Workbooks.Open(YeniSB) 

YeniStoryBoard.Worksheets("Storyboard").Copy After:=ThisWorkbook.Worksheets("Kunye") 
YeniStoryBoard.Close 

Set YeniStoryBoard_isim = Sheets("Storyboard") 
YeniStoryBoard_isim.Name = "StoryboardXXYYZZ" 
Application.EnableEvents = True 

    End With 
End Sub 

非常感謝。 :)

回答

1

我想你應該使用此解決方案打開

Application.EnableEvents = False   'disable Events 
Set YeniStoryBoard = Workbooks.Open(YeniSB) 'open workbook 
Application.EnableEvents = True    'enable Events 

爲了確保工作簿禁用事件之後禁用它們,您可以使用額外的語句

YeniStoryBoard.Application.EnableEvents = False 
'Do something 
YeniStoryBoard.Application.EnableEvents = True 
YeniStoryBoard.Close 
+0

太謝謝你了。這次excel給了我這個錯誤:「Path not found:'\ VBA813.tmp' – Bildircin13

+0

@ Bildircin13在哪裏放新代碼?使用你的代碼,我可以正常運行 – hung34k

+0

這段代碼是在一個新的模塊中,但它給了我路徑不幸的是,我錯過了 – Bildircin13