gpt4 book ai didi

excel - 运行时错误1004;错误处理

转载 作者:行者123 更新时间:2023-12-03 08:28:50 25 4
gpt4 key购买 nike

我的错误处理有问题。我正在将一系列编号文件缝合在一起,以用于多个营业地点。我已经编写了代码,以针对“文件不存在”的错误移至下一个位置循环,因为每个位置具有不同数量的文件。

即使进行了错误处理,我仍然遇到运行时1004错误。怎么会这样?它在第二个定位环上炸弹,在第一个定位环上工作。为了安全起见,我使用了15个文件的循环,因为我遇到的最大值是12。

    Sub OpenURL()

Dim LocBackupFile As String
Dim CurrentFile As String
Dim HubFileName As String

Application.DisplayAlerts = False
Filedate = Format(Date, "mm-dd-yyyy")

'Cycle through Hubs
HubArray = Array("GA100%20-%20AHUB", "TX100%20-%20DHUB", "CA200%20-%20HHUB", "IN100%20-%20IHUB", "WA100%20-%20KHUB", _
"AB100%20-%20LHUB", "MO100%20-%20MHUB", "NC100%20-%20NHUB", "OH100%20-%20OHUB", "PA100%20-%20SHUB", _
"IN200%20-%20THUB", "UT100%20-%20UHUB", "ON100%20-%20VHUB", "MN100%20-%20WINO", "NL100%20-%20YHUB")

For Hub = LBound(HubArray) To UBound(HubArray)

HubName = Left(HubArray(Hub), 5)
HubFileName = HubName & " NoLocBackup " & Filedate & ".xlsb"

For CheckAndOpen = 1 To 15

LocBackupFile = "http://fastnet.--------.com/sites/fastreports/Hubs/" & HubArray(Hub) & "/locbackup_ws" & CheckAndOpen & ".xls"

On Error Resume Next
Workbooks.Open FileName:=LocBackupFile
On Error GoTo Done

CurrentFile = "locbackup_ws" & CheckAndOpen & ".xls"
If CheckAndOpen = 1 Then
RowCount = ActiveSheet.UsedRange.Rows.Count
Workbooks.Add.SaveAs FileName:="R:\" & HubFileName, FileFormat:=50
If RowCount >= 65000 Then
DestRowCount = 65001
End If
Workbooks(CurrentFile).Sheets(1).Range("A1:H" & RowCount).Copy Destination:=Workbooks(HubFileName).Sheets(1).Range("A1")
Else
RowCount = ActiveSheet.UsedRange.Rows.Count
If RowCount < 64999 Then
Workbooks(CurrentFile).Sheets(1).Range("A3:H" & RowCount).Copy Destination:=Workbooks(HubFileName).Sheets(1).Range("A" & DestRowCount)
Else
Workbooks(CurrentFile).Sheets(1).Range("A3:H65000").Copy Destination:=Workbooks(HubFileName).Sheets(1).Range("A" & DestRowCount)
DestRowCount = DestRowCount + 64998
End If
End If

Workbooks(CurrentFile).Close SaveChanges:=False

Next CheckAndOpen

Done:

On Error GoTo 0

Workbooks(HubFileName).Save
Workbooks(HubFileName).Close

Next Hub

Application.DisplayAlerts = True

End Sub

最佳答案

我遇到了同样的问题(错误处理未捕获错误1004)。
这是我的解决方法:
我将代码放在For循环内的单独“子”子(即process_file)中,并将所有必需的参数传递给它。
为您服务:在“母亲”子项OpenURL()中,您将拥有:

On Error GoTo err_line 'or do something else
For CheckAndOpen = 1 To 15
has_err = True
Call process_file( all required parameters here)
has_err = False
err_line:
if has_err then
'your handling code goes here
End If
Next
在子OpenURL()外部,您将拥有:
Sub process_file(all required parameters here)
'...code from the for in the mother....
End sub
child-sub process_file中的错误处理仍将起作用,但是如果一切失败,母亲将继续工作。
编辑
您可能希望使用此方法仅捕获破坏代码的行(仅将该行放在子子程序中,并在父子程序中进行错误处理)。此外,这是一种解决方法,而不是对该问题的实际解决方案。

关于excel - 运行时错误1004;错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32506125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com