我尝试打开文件夹中的所有文件,然后从这些文件中复制数据并将其添加到单个工作表中,关闭该文件,然后将其移动到新文件夹中。我对VBA还比较陌生,以下是我目前所拥有的功能:打开对话框并选择文件夹,打开工作簿,然后关闭工作簿。然而,当代码继续打开工作簿时,我的excel只是出现故障并被冻结。请帮助。
Sub OpenFilesinFolderModWorkingDoc()
'create reference workbook variables
Dim FolderPath As String 'path of folder
Dim CPath As String 'path for current workbooks
Dim CName As String 'name for current workbooks
Dim DiaFolder As FileDialog
Dim mwb As Workbook 'individual workbooks
'Turn off settings
Application.ScreenUpdating = False
'File Dialogue
Set DiaFolder = Application.FileDialog(msoFileDialogFolderPicker)
DiaFolder.AllowMultiSelect = False
DiaFolder.Show
FolderPath = DiaFolder.SelectedItems(1)
CPath = FolderPath & "\" ' location of files
CName = Dir(CPath & "*.xlsx")
'loop through files in folder
Do While CName <> "" 'Loop through all files in selected folder
Set mwb = Workbooks.Open(CPath & "\" & CName)
mwb.Close True
Loop
End Sub
3条答案
按热度按时间ykejflvf1#
只是一些思考的食物,我在过去做过类似的事情,但它是与Excel中的Power Query(数据-查询和连接),不确定这是否是一个选项,为您或没有。它可以将多个文件合并成一个,然后使用Power Automate您可以将文件移动到另一个目录。
nbysray52#
我实际上在我的机器上尝试了你的确切代码,它发生了故障,也让我感到惊讶,因为代码看起来不错。我放慢了速度,我认为这可能是由于文件被存储在OneDrive(MS云)上,而不是将其保存在我的本地硬盘上。
我的问题是,它一直试图立即保存,这是行为时,在一个驱动器,因为它保存在真实的。
尝试在下载中的本地目录或未与Microsoft OneDrive同步的任何文件夹上进行测试。
svmlkihl3#
遍历选定文件夹(
FileDialog
)的文件(Dir
)