我已經創建了一個腳本,它是在單擊按鈕上啓動的,因此它從路徑和格式加載CSV文件並將其保存。因爲我有1 ... 10000的文件,所以我創建了像1.txt等文件。 我得到的問題是,當它啓動過程中一切順利,但在10-13文件後,它給錯誤和Excel關閉。以下是我正在使用的代碼。請幫助我,我在哪裏做錯了。我想我在數組中犯了錯誤,我嘗試了redim,但是那個給了我同樣的錯誤。這是我放在我的按鈕啓動過程的子。由於我有超過數千個文件,所以請建議我解決方案。Excel保存10個文件後給我錯誤
Sub WorkbooksLoop()
' get the list of filenames
Dim pageStart As Integer
Dim pageEnd As Integer
pageStart = CInt(Cells(3, "C").Value) ' getting from cell of excel sheet
pageEnd = CInt(Cells(4, "C").Value) ' getting from cell of excel sheet
Dim Filenames(44) As String ' variable I know there are 44 files
For j = pageStart To pageEnd
Filenames(j) = CStr(j) + ".txt"
Next j
On Error GoTo NoFilenames
Dim controllerwb As Workbook
Set controllerwb = ActiveWorkbook
Dim wb As Workbook
Dim fname As Variant
Dim rootPath As String
rootPath = ThisWorkbook.Path
rootPath = rootPath & "\"
For Each fname In Filenames
' Make the controller active
controllerwb.Activate
On Error Resume Next
' If activate fails, then the workbook isn't open
Workbooks(fname).Activate
' If activate fails, then the workbook isn't open
If Err <> 0 Then
OpenFile (rootPath & fname)
Set wb = ActiveWorkbook
wb.Activate
' Otherwise, workbook is already open, refer to it by name
Else
OpenFile (rootPath & fname)
Set wb = ActiveWorkbook
End If
' do something to the open workbook my process to format sheet
deletingRowsColumns
ledgerSetup
resizeColumns
columnLines
columnAlignments
mergeTitles
settingNames
wb.Close
Next fname
NoFilenames:
End Sub
您不恰當地使用'On Error Resume Next'可能會隱藏正在調用的程序中發生的錯誤,例如'deletedRowsColumns','ledgerSetup'等。 –
我建議添加'On Error GoTo 0'立即*在* End If語句之後。這將恢復正常的錯誤處理,並幫助識別任何其他被調用的過程中的錯誤。 –
還要考慮描述錯誤引發的行,以及您正在接收的具體錯誤消息。 –