VBA选择多个文件路径并保存在单元格中

时间:2019-10-28 21:31:16

标签: excel vba

我可以将一个文件路径保存到所需的单元格中,我希望一次可以保存多个文件路径。我试图更改代码,但是我不知道如何保存到下一行,而只是将所有内容保存在同一行。

Dim FileFldr As FileDialog
Dim FileName, OrigFilePath, FileType, FilePath, CustID As String
Dim LastAttRow As Long
Dim vrtSelectedItem As Variant
CustID = txtID.Value
Set FileFldr = Application.FileDialog(msoFileDialogFilePicker)

With FileFldr
   .AllowMultiSelect = True
   .Title = "Select file to attach"
   .Filters.Add "All Files", "*.*", 1
   If .Show <> -1 Then GoTo NoSelection

   For Each vrtSelectedItem In .SelectedItems
   Next

    FilePath = .SelectedItems()
    FileName = Dir(FilePath)
    FileType = Right(FileName, Len(FileName) - InStr(Dir(FileName), "."))

   With Sheet6
        LastAttRow = .Range("D9999").End(xlUp).Row + 1 
        .Range("D" & LastAttRow).Value = CustID 
        .Range("E" & LastAttRow).Value = FileName
        .Range("F" & LastAttRow).Value = FileType
        .Range("G" & LastAttRow).Value = FilePat
        .Range("H" & LastAttRow).Value = "=Row()"

   End With 
   NoSelection:
   End With 
   Sheet6.Activate

End Sub

2 个答案:

答案 0 :(得分:0)

这是一种类似的方法,会将每个文件的属性放在D到H列中的自己行上。

请注意,如果要运行两次,第二次选择的内容将覆盖已经存在的内容,并且不会追加到末尾。 要更改此行为,您将需要另一个变量来保存和保留文件计数,然后在下次将数组写入表时,从上一个文件计数+1开始范围,然后是上一个文件计数的结束范围+新文件。计数。

    Sub newtest()

    Dim FileFldr As FileDialog
    Dim FileName, OrigFilePath, FileType, FilePath, CustID As String

    CustID = txtID.Value

    Set FileFldr = Application.FileDialog(msoFileDialogFilePicker)

    With FileFldr
       .AllowMultiSelect = True
       .Title = "Select file to attach"
       .Filters.Add "All Files", "*.*", 1
       If .Show <> -1 Then GoTo NoSelection

        Dim ArrayCount As Long: RowCount = 0 'counter to hold what number in the array your on : 'set this number to 0 where array starts
        Dim FileArray As Variant 'declare the array which will hold all the files user selected
        Dim FileCount As Long: FileCount = .SelectedItems.Count 'declare the long which will hold the amount of files the user has selected - set to the amount of selected files
            ReDim FileArray(FileCount, 4) 'redim the array to the size of the amount of files

       'loop through each of the selected items and assign its values to an address in the array
       For Each vrtSelectedItem In .SelectedItems
            FilePath = vrtSelectedItem
            FileName = Dir(FilePath)
            FileType = Right(FileName, Len(FileName) - InStr(Dir(FileName), "."))

            FileArray(ArrayCount, 0) = CustID
            FileArray(ArrayCount, 1) = FileName
            FileArray(ArrayCount, 2) = FileType
            FileArray(ArrayCount, 3) = FilePath
            FileArray(ArrayCount, 4) = "=Row()"
            ArrayCount = ArrayCount + 1
       Next

        With Sheet6
            .Range(Cells(1, 4), Cells(FileCount, 8)).Value = FileArray 'set range to what the array is and then put values from array in that range
        End With

    NoSelection:
       End With
       Sheet6.Activate

    End Sub

答案 1 :(得分:0)

只需几处更改即可返回您的代码。我认为它可以满足您的需求:

$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "jekyll-feed":
  In Gemfile:
    jekyll-feed (~> 0.12)

    github-pages (= 202) was resolved to 202, which depends on
      jekyll-feed (= 0.11.0)