首页 > 解决方案 > 读取和列出用户输入的文件位置并在错误时退出的宏

问题描述

我目前有一个宏,可以读取用户输入的某些文件,下面的部分代码收集文件名。但是,当用户输入多个文件位置时,只会输入一个文件位置。

在 C 列中,我想输入所有文件位置的顺序,以便它们被列为:filelocation1、filelocaction2...等等。

这是我当前的代码:

Sub openfiles()

    Dim Files As Variant

    'open directory, filter by excel files, select files and place in array
    SelectedFiles = Application.GetOpenFilename( _
        FileFilter:="Excel Filter (*.xlsx), *.xlsx", _
        MultiSelect:=True)

    Application.ScreenUpdating = False 'update in background

    If not IsArray(Files) Then
        MsgBox "No files selected. Cannot continue."
        Range("O" & Rows.Count).End(xlUp).Offset(1, 0) = "no files selected"
        Exit Sub
    Else
        Range("O" & Rows.Count).End(xlUp).Offset(1, 0) = Files
    End If

End Sub

标签: excelvba

解决方案


推荐阅读