选择多个文件以打开-VBA [英] Select multiple files to open -VBA
问题描述
我发现以下代码用于打开多个文件,与原始文件略有不同。我知道For Loop中可以做很多事情。 但是,我需要引用每个单独的工作簿进行单独分析
,并希望了解它的方法。
I found the following code for opening multiple files with a little change from the original. I understand that a lot of things can be done within the For Loop. However, I need to reference each individual Workbook for separate analysis and would like to learn the way for it.
Sub OpenSeveralFiles ()
Sub OpenSeveralFiles()
Dim fd 作为FileDialog
Dim FileChosen 作为整数
Dim FileName 作为字符串
Dim i 作为整数
Dim fd As FileDialog
Dim FileChosen As Integer
Dim FileName As String
Dim i As Integer
设置fd = Application.FileDialog(msoFileDialogFilePicker)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialView = msoFileDialogViewList
fd.InitialView = msoFileDialogViewList
fd.AllowMultiSelect = True
FileChosen = fd.Show
fd.AllowMultiSelect = True
FileChosen = fd.Show
如果FileChosen = -1则为
对于i = 1到fd.SelectedItems.Count
Workbooks.Open fd.SelectedItems(i)
If FileChosen = -1 Then
For i = 1 To fd.SelectedItems.Count
Workbooks.Open fd.SelectedItems(i)
工作表("Sheet1")。单元格(1,1)="Go!"
Worksheets("Sheet1").Cells(1, 1) = "Go!"
工作表("Sheet1")。单元格(2,1)= fd.SelectedItems(i)
  ;  接下来我是
结束如果
结束子
Worksheets("Sheet1").Cells(2, 1) = fd.SelectedItems(i)
Next i
End If
End Sub
推荐答案
在不知道你究竟想要对工作簿做什么的情况下,很难具体,但你会处理工作簿顺序例如
Without knowing what exactly you are trying to do with the workbooks, it is difficult to be specific, but you would process the workbooks sequentially e.g.
Sub OpenSeveralFiles()
Dim fd As FileDialog
Dim FileChosen As Integer
Dim FileName As String
Dim xlBook As Workbook
Dim i As Integer
设置fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialView = msoFileDialogViewList
fd.AllowMultiSelect = True
FileChosen = fd.Show
如果FileChosen = -1则为
对于i = 1到fd.SelectedItems.Count
设置xlBook = Workbooks.Open(fd.SelectedItems(i))
'用xlbook做你想做的事,例如
使用xlBook
MsgBox。名称
.Worksheets(" Sheet1")。Cells(1,1)=" Go!"
$
      .Worksheets(" Sheet1")。Cells(2,1)= fd.SelectedItems(i)
      .Close 1 '关闭图书并保存更改
结束与$
接下来我是
结束如果
结束次要
Sub OpenSeveralFiles()
Dim fd As FileDialog
Dim FileChosen As Integer
Dim FileName As String
Dim xlBook As Workbook
Dim i As Integer
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialView = msoFileDialogViewList
fd.AllowMultiSelect = True
FileChosen = fd.Show
If FileChosen = -1 Then
For i = 1 To fd.SelectedItems.Count
Set xlBook = Workbooks.Open(fd.SelectedItems(i))
'do what you want with xlbook e.g
With xlBook
MsgBox .Name
.Worksheets("Sheet1").Cells(1, 1) = "Go!"
.Worksheets("Sheet1").Cells(2, 1) = fd.SelectedItems(i)
.Close 1 'Close the book and save the changes
End With
Next i
End If
End Sub
这篇关于选择多个文件以打开-VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!