Excel 离子手册 .ScreenUpdate=False .EnableEvents=False 以 '循环遍历数组中的所有文件(myFiles) 如果Fnum>0,则 对于Fnum=LBound(MyFiles)到UBound(MyFiles) 设置mybook=Nothing 出错时继续下一步 设置mybook=Workbooks.Open(MyPath&MyFiles(Fnum)) 错误转到0 如果不是的话,我的书什么都不是 '更改mybook中一个工作表中的单元格值 出错时继续下一步 '将您的代码放在这里。 如果错误编号>0,则 ErrorYes=True 呃,明白了 '关闭我的书本而不保存 mybook.Close savechanges:=False 其他的 '保存并关闭mybook mybook.Close savechanges:=True 如果结束 错误转到0 其他的 '无法打开工作簿 ErrorYes=True 如果结束 下一个Fnum 如果结束 如果ErrorYes=True,则 MsgBox“一个或多个文件中存在问题,可能存在问题:”_ &vbNewLine&“受保护的工作簿/工作表或不存在的工作表/范围” 如果结束 '还原屏幕更新、计算和启用事件 应用 .ScreenUpdate=True .EnableEvents=True .Calculation=CalcMode 以 端接头
感谢您的帮助,但是,即使使用这三个步骤,当我单击“运行”时,它也是即时的,不会发生任何事情。我希望几分钟内处理10000多个文件。您确定它是Excel 离子手册 .ScreenUpdate=False .EnableEvents=False 以 '循环遍历数组中的所有文件(myFiles) 如果Fnum>0,则 对于Fnum=LBound(MyFiles)到UBound(MyFiles) 设置mybook=Nothing 出错时继续下一步 设置mybook=Workbooks.Open(MyPath&MyFiles(Fnum)) 错误转到0 如果不是的话,我的书什么都不是 '更改mybook中一个工作表中的单元格值 出错时继续下一步 '将您的代码放在这里。 如果错误编号>0,则 ErrorYes=True 呃,明白了 '关闭我的书本而不保存 mybook.Close savechanges:=False 其他的 '保存并关闭mybook mybook.Close savechanges:=True 如果结束 错误转到0 其他的 '无法打开工作簿 ErrorYes=True 如果结束 下一个Fnum 如果结束 如果ErrorYes=True,则 MsgBox“一个或多个文件中存在问题,可能存在问题:”_ &vbNewLine&“受保护的工作簿/工作表或不存在的工作表/范围” 如果结束 '还原屏幕更新、计算和启用事件 应用 .ScreenUpdate=True .EnableEvents=True .Calculation=CalcMode 以 端接头,excel,vba,csv,Excel,Vba,Csv,感谢您的帮助,但是,即使使用这三个步骤,当我单击“运行”时,它也是即时的,不会发生任何事情。我希望几分钟内处理10000多个文件。您确定它是.xls而不是.xlsx或.xlsm文件吗?如果它是.xls/.xlsx/.xlsm的混合体,那么将行更改为目录(路径名“*.xls*”)是否使用了我的子工作(wb作为工作簿)如上图所示?让我们来看看。感谢您的帮助,但是,即使使用这三个步骤,当我单击“运行”时,它也是即时的,不会发生任何事情。我希望几分钟内处理10000多个文件。您确定它是.xls而不是.x
.xls
而不是.xlsx
或.xlsm
文件吗?如果它是.xls/.xlsx/.xlsm
的混合体,那么将行更改为目录(路径名“*.xls*”)
是否使用了我的子工作(wb作为工作簿)
如上图所示?让我们来看看。感谢您的帮助,但是,即使使用这三个步骤,当我单击“运行”时,它也是即时的,不会发生任何事情。我希望几分钟内处理10000多个文件。您确定它是.xls
而不是.xlsx
或.xlsm
文件吗?如果它是.xls/.xlsx/.xlsm
的混合体,那么将行更改为目录(路径名“*.xls*”)
是否使用了我的子工作(wb作为工作簿)
如上所示?让我们来看看可能的重复
Sub toColumns()
'
' toColumns Macro
' Changes csv to columns
'
' Keyboard Shortcut: Ctrl+a
'
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True
End Sub
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "H:\Macro\positions"
Filename = Dir(Pathname & "*.xls")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
DoWork wb
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
Sub DoWork(wb As Workbook)
With wb
'Do your work here
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True
End With
End Sub
Sub DoWork(wb As Workbook)
With wb.Sheets(1) '<~~ Or change this to the relevant sheet number
.Columns(1).TextToColumns Destination:=.Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True
End With
End Sub
Sub Example()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String, Fnum As Long
Dim mybook As Workbook
Dim CalcMode As Long
Dim sh As Worksheet
Dim ErrorYes As Boolean
'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
'Change cell value(s) in one worksheet in mybook
On Error Resume Next
' PUT YOUR CODE RIGHT HERE . . .
If Err.Number > 0 Then
ErrorYes = True
Err.Clear
'Close mybook without saving
mybook.Close savechanges:=False
Else
'Save and close mybook
mybook.Close savechanges:=True
End If
On Error GoTo 0
Else
'Not possible to open the workbook
ErrorYes = True
End If
Next Fnum
End If
If ErrorYes = True Then
MsgBox "There are problems in one or more files, possible problem:" _
& vbNewLine & "protected workbook/sheet or a sheet/range that not exist"
End If
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub