引用在vba中使用filedialog选择的工作簿

时间:2017-07-28 06:33:25

标签: excel vba excel-vba reference

我正在尝试在Excel中创建一种用户界面,并且需要找到一个简单的解决方案,将工作表从选定的工作簿复制到特定的工作簿。到目前为止,我已经编写了下面的代码,但我不知道如何在代码中引用所选的工作簿。 用户需要将工作表(名为Storyboard并且名称永不更改)从许多不同的工作簿(具有不同的名称)复制到具有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 个答案:

答案 0 :(得分: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