Excel/VBA 循环只在第一个文件上执行
Excel/VBA loop only executes on the first file
我在一个文件夹下有大约 100 个 .xls
文件,我有一个宏脚本来遍历每个文件以进行一些数据处理。 objective是将每个工作簿拆分为三个,名称分别为N1
、N2
、N3
。到目前为止,我的 SplitData
宏运行良好,但我对提取的工作簿有疑问。
我想将新提取的三个工作簿合并到已经存在的工作簿中,而不是每次都收到像 "File N1 already exists." 这样的警报。由于我之前问题的建议答案,我更改了 Application.DisplayAlerts = false
,但现在我收到了一个新错误:
禁用警报后,我的前两个提取的工作簿不断更新与我开始提取的第一个工作簿相同的结果,而第三个工作簿陷入循环,从起始工作簿添加相同的结果。我假设我的循环有问题但找不到它,请谁能帮我检查一下?
非常感谢!
这是我循环遍历文件夹的代码:
Sub OpenFiles()
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xls")
Do While xFile <> ""
Call SplitData
Loop
End Sub
这是 SplitData 宏:
Sub SplitData()
' 1. Fill every cells in merged columns for future steps
Dim cell As Range, joinedCells As Range
For Each cell In Range("E4:I60")
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
' 2. Split original sheet into three based on one col value
' loop through selected column to check if has different values
Const NameCol = "B"
Const HeaderRow = 3
Const FirstRow = 4
Dim SrcSheet As Worksheet
Dim TrgSheet As Worksheet
Dim SrcRow As Long
Dim LastRow As Long
Dim TrgRow As Long
Dim Student As String
Application.ScreenUpdating = False
Set SrcSheet = ActiveSheet
LastRow = SrcSheet.Cells(SrcSheet.Rows.Count, NameCol).End(xlUp).Row
For SrcRow = FirstRow To LastRow
Student = SrcSheet.Cells(SrcRow, NameCol).Value
Set TrgSheet = Nothing
On Error Resume Next
Set TrgSheet = Worksheets(Student)
On Error GoTo 0
If TrgSheet Is Nothing Then
Set TrgSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
TrgSheet.Name = Student
SrcSheet.Rows(HeaderRow).Copy Destination:=TrgSheet.Rows(HeaderRow)
End If
TrgRow = TrgSheet.Cells(TrgSheet.Rows.Count, NameCol).End(xlUp).Row + 1
SrcSheet.Rows(SrcRow).Copy Destination:=TrgSheet.Rows(TrgRow)
Next SrcRow
Application.ScreenUpdating = True
' 3. Extract three new worksheets into three workbooks
Dim Pointer As Long
Set MainWorkBook = ActiveWorkbook
Range("E4").Value = MainWorkBook.Sheets.Count
Application.ScreenUpdating = False 'enhance the performance
For Pointer = 2 To MainWorkBook.Sheets.Count
Set NewWorkbook = Workbooks.Add
MainWorkBook.Sheets(Pointer).Copy After:=NewWorkbook.Sheets(1)
Application.DisplayAlerts = False
NewWorkbook.Sheets(1).Delete
Application.DisplayAlerts = False
With NewWorkbook
.SaveAs Filename:="D:\***\Inventory\" & MainWorkBook.Sheets(Pointer).Name & ".xls"
End With
NewWorkbook.Close SaveChanges:=True
Next Pointer
Application.ScreenUpdating = True
End Sub
您需要在循环中添加 xFile = Dir
以循环浏览文件。
...
xFile = Dir(xStrPath & "\*.xls")
Do While xFile <> ""
Call SplitData
xFile = Dir
Loop
...
目前还不清楚 xFile 是如何传递给 SplitData 的。 SplitData 不应该有一个接收 xFile 的参数吗?
好像要打开和关闭文件。
Sub OpenFiles()
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xls")
Dim Wb As Workbook
Do While xFile <> ""
Set Wb = Workbooks.Open(Filename:=xStrPath & "\" & xFile) '<~~ open file
Call SplitData
Wb.Close (0) '<~~ close file
xFile = Dir '<~~ re dir
Loop
End Sub
我在一个文件夹下有大约 100 个 .xls
文件,我有一个宏脚本来遍历每个文件以进行一些数据处理。 objective是将每个工作簿拆分为三个,名称分别为N1
、N2
、N3
。到目前为止,我的 SplitData
宏运行良好,但我对提取的工作簿有疑问。
我想将新提取的三个工作簿合并到已经存在的工作簿中,而不是每次都收到像 "File N1 already exists." 这样的警报。由于我之前问题的建议答案,我更改了 Application.DisplayAlerts = false
,但现在我收到了一个新错误:
禁用警报后,我的前两个提取的工作簿不断更新与我开始提取的第一个工作簿相同的结果,而第三个工作簿陷入循环,从起始工作簿添加相同的结果。我假设我的循环有问题但找不到它,请谁能帮我检查一下?
非常感谢!
这是我循环遍历文件夹的代码:
Sub OpenFiles()
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xls")
Do While xFile <> ""
Call SplitData
Loop
End Sub
这是 SplitData 宏:
Sub SplitData()
' 1. Fill every cells in merged columns for future steps
Dim cell As Range, joinedCells As Range
For Each cell In Range("E4:I60")
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
' 2. Split original sheet into three based on one col value
' loop through selected column to check if has different values
Const NameCol = "B"
Const HeaderRow = 3
Const FirstRow = 4
Dim SrcSheet As Worksheet
Dim TrgSheet As Worksheet
Dim SrcRow As Long
Dim LastRow As Long
Dim TrgRow As Long
Dim Student As String
Application.ScreenUpdating = False
Set SrcSheet = ActiveSheet
LastRow = SrcSheet.Cells(SrcSheet.Rows.Count, NameCol).End(xlUp).Row
For SrcRow = FirstRow To LastRow
Student = SrcSheet.Cells(SrcRow, NameCol).Value
Set TrgSheet = Nothing
On Error Resume Next
Set TrgSheet = Worksheets(Student)
On Error GoTo 0
If TrgSheet Is Nothing Then
Set TrgSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
TrgSheet.Name = Student
SrcSheet.Rows(HeaderRow).Copy Destination:=TrgSheet.Rows(HeaderRow)
End If
TrgRow = TrgSheet.Cells(TrgSheet.Rows.Count, NameCol).End(xlUp).Row + 1
SrcSheet.Rows(SrcRow).Copy Destination:=TrgSheet.Rows(TrgRow)
Next SrcRow
Application.ScreenUpdating = True
' 3. Extract three new worksheets into three workbooks
Dim Pointer As Long
Set MainWorkBook = ActiveWorkbook
Range("E4").Value = MainWorkBook.Sheets.Count
Application.ScreenUpdating = False 'enhance the performance
For Pointer = 2 To MainWorkBook.Sheets.Count
Set NewWorkbook = Workbooks.Add
MainWorkBook.Sheets(Pointer).Copy After:=NewWorkbook.Sheets(1)
Application.DisplayAlerts = False
NewWorkbook.Sheets(1).Delete
Application.DisplayAlerts = False
With NewWorkbook
.SaveAs Filename:="D:\***\Inventory\" & MainWorkBook.Sheets(Pointer).Name & ".xls"
End With
NewWorkbook.Close SaveChanges:=True
Next Pointer
Application.ScreenUpdating = True
End Sub
您需要在循环中添加 xFile = Dir
以循环浏览文件。
...
xFile = Dir(xStrPath & "\*.xls")
Do While xFile <> ""
Call SplitData
xFile = Dir
Loop
...
目前还不清楚 xFile 是如何传递给 SplitData 的。 SplitData 不应该有一个接收 xFile 的参数吗?
好像要打开和关闭文件。
Sub OpenFiles()
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xls")
Dim Wb As Workbook
Do While xFile <> ""
Set Wb = Workbooks.Open(Filename:=xStrPath & "\" & xFile) '<~~ open file
Call SplitData
Wb.Close (0) '<~~ close file
xFile = Dir '<~~ re dir
Loop
End Sub