gpt4 book ai didi

vba - 使用 FileDialog 打开工作簿并在 Excel VBA 中对其进行操作

转载 作者:行者123 更新时间:2023-12-01 17:53:50 24 4
gpt4 key购买 nike

我正在学习如何使用 Excel 宏,我发现了以下代码:

Dim fd As Office.FileDialog

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd

.AllowMultiSelect = False
.Title = "Please select the file to kill his non colored cells"
.Filters.Add "Excel", "*.xls"
.Filters.Add "All", "*.*"

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

End With

此代码打开文件对话框。如何打开选定的 Excel 文件而不覆盖之前打开的文件?

最佳答案

谢谢弗兰克,我明白了。这是工作代码。

Option Explicit
Private Sub CommandButton1_Click()

Dim directory As String, fileName As String, sheet As Worksheet, total As Integer
Dim fd As Office.FileDialog

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls?"

If .Show = True Then
fileName = Dir(.SelectedItems(1))

End If
End With

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Workbooks.Open (fileName)

For Each sheet In Workbooks(fileName).Worksheets
total = Workbooks("import-sheets.xlsm").Worksheets.Count
Workbooks(fileName).Worksheets(sheet.Name).Copy _
after:=Workbooks("import-sheets.xlsm").Worksheets(total)
Next sheet

Workbooks(fileName).Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

关于vba - 使用 FileDialog 打开工作簿并在 Excel VBA 中对其进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com