我有一个允许我在excel工作簿中打开文件的代码,但是我希望能够在同一个工作簿中打开多个文件,名为p00001,p00002,p00003等等。有谁知道我如何编辑我的代码来选择所有这样命名的文件,并在同一工作簿的单独工作表中打开它们?
我的代码是:
Sub Open_Workbook()
Dim my_FileName As Variant
my_FileName = Application.GetOpenFilename
If my_FileName <> False Then
Workbooks.Open Filename:=my_FileName
End If
End Sub
答案 0 :(得分:3)
在此解决方案中,我使用FileDialog选择多个文件。 在那之后,你需要循环所有的文件a for循环。 在For循环中,您必须打开文件并导入工作表。在此示例中,我导入了工作簿中的所有工作表。 代码完成后导入关闭源工作簿并为其余文件执行相同操作。
<TextView
android:id="@+id/productTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
如果你想从目录中获取所有文件,你可以用Loop更改ApplicationFile对话框,你可以像这样循环目录:
Sub Import Files()
Dim sheet As Worksheet
Dim total As Integer
Dim intChoice As Integer
Dim strPath As String
Dim i As Integer
Dim wbNew As Workbook
Dim wbSource As Workbook
Set wbNew = Workbooks.Add
'allow the user to select multiple files
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
For i = 1 To Application.FileDialog(msoFileDialogOpen).SelectedItems.Count
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(i)
Set wbSource = Workbooks.Open(strPath)
For Each sheet In wbSource.Worksheets
total = wbNew.Worksheets.Count
wbSource.Worksheets(sheet.Name).Copy _
after:=wbNew.Worksheets(total)
Next sheet
wbSource.Close
Next i
End If
End Sub