【发布时间】:2017-11-09 15:45:46
【问题描述】:
这是我第一次发帖,如有错误请见谅。
我在工作簿中运行以下代码来打开其他 3 个工作簿,从中复制并粘贴到原始工作簿中。
我已设置错误处理以捕获哪个工作表引发错误(如果有)以在故障排除时提供帮助。
对于第一张 2 张,一切正常。通常由于命名错误而无法找到文件时会引发错误。
但是,第三个文件“wellsFile”触发了错误处理并给了我一个
1004 对象范围_Global 失败
特别是在With Range("O34") 行。
据我所知,i=3 错误处理部分与其他 2 部分相同。
我已尝试使用 activesheet 和 activeworkbook 完全限定范围选择,并已定义应用程序的错误更改。我尝试了其他范围,这也会引发错误。
我知道 wbk.activate 和工作表激活正在工作,因为它们确实将我带到了我想要的地方。
感谢您的帮助。
Sub rateImport()
'created by Brad Chandler
'7-21-17
'goal is to take the rate sheets from desktop, copy and paste certain sections to certain sections
'of the Pricing Input sheet
Dim conName As String
Dim govName As String
Dim wellName As String
Dim conFile As String
Dim govFile As String
Dim wellsFile As String
Dim govWbk As Workbook
Dim conWbk As Workbook
Dim wellsWbk As Workbook
Dim Wbk As Workbook
Dim i As Integer 'for error assistance
On Error GoTo errorhandler
Application.ScreenUpdating = False
'setting up Con and Gov file names for opening
conName = Trim(Sheets("Dashboard").Range("O32").Value) 'removes beginning and ending spaces
conFile = "C:\Users\" & (Environ$("Username")) & "\Desktop\" & conName
govName = Trim(Sheets("Dashboard").Range("O33").Value)
govFile = "C:\Users\" & (Environ$("Username")) & "\Desktop\" & govName
wellName = Trim(Sheets("Dashboard").Range("O34").Value)
wellsFile = "C:\Users\" & (Environ$("Username")) & "\Desktop\" & wellName
'opening and copying rates over
Set Wbk = ActiveWorkbook
Wbk.Sheets("Pricing Input").Activate
'copying from Conventional
i = 1
Workbooks.Open Filename:=conFile
Set conWbk = ActiveWorkbook
conWbk.Sheets("CSV File").Activate
'30 year Copy
Range("B2:F14").Copy
Wbk.Activate
Range("A3").PasteSpecial xlPasteValues
conWbk.Activate
'20 year copy
Range("B16:F29").Copy
Wbk.Activate
Range("A23").PasteSpecial xlPasteValues
conWbk.Activate
'15 year copy
Range("B31:F43").Copy
Wbk.Activate
Range("A43").PasteSpecial xlPasteValues
'preventing save message from showing
Application.DisplayAlerts = False
conWbk.Close
Application.DisplayAlerts = True
'copying from Government
i = 2
Workbooks.Open Filename:=govFile
Set govWbk = ActiveWorkbook
govWbk.Sheets("CSV File").Activate
'30 year copy
Range("B5:F21").Copy
Wbk.Activate
Range("L3").PasteSpecial xlPasteValues
govWbk.Activate
'15 year copy
Range("B31:F47").Copy
Wbk.Activate
Range("L23").PasteSpecial xlPasteValues
'prevening save message from showing
Application.DisplayAlerts = False
govWbk.Close
Application.DisplayAlerts = True
'opening and copying Wells Rates
Wbk.Sheets("Wells Pricing").Activate
i = 3
Workbooks.Open Filename:=wellsFile
Set wellsWbk = ActiveWorkbook
wellsWbk.Sheets("Conf Pricing").Activate
'30,20,15 Con copy
Range("B21:J49").Copy
Wbk.Activate
Range("A1").PasteSpecial xlPasteValues
'lock term pricing
wellsWbk.Activate
Range("B51:H62").Copy
Wbk.Activate
Range("A31").PasteSpecial xlPasteValues
wellsWbk.Sheets("Govt").Activate
'FHA VA pricing copy
Range("B14:K39").Copy
Wbk.Activate
Range("N1").PasteSpecial xlPasteValues
wellsWbk.Activate
'USDA pricing copy
Range("B87:F105").Copy
Wbk.Activate
Range("N29").PasteSpecial xlPasteValues
'preventing save message from showing
Application.DisplayAlerts = False
wellsWbk.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Exit Sub
errorhandler:
If i = 1 Then
Wbk.Activate
Sheets("Dashboard").Activate
With Range("O32")
.Select
.Font.Bold = True
.Interior.Color = RGB(253, 123, 151)
End With
Exit Sub
Else
If i = 2 Then
Wbk.Activate
Sheets("Dashboard").Activate
With Range("O33")
.Select
.Font.Bold = True
.Interior.Color = RGB(253, 123, 151)
End With
Exit Sub
Else
If i = 3 Then
Wbk.Activate
Sheets("Dashboard").Activate
**With Range("034")**
.Select
.Font.Bold = True
.Interior.Color = RGB(253, 123, 151)
End With
Exit Sub
End If
End If
End If
End Sub
*我已经删除了一些不相关的代码
【问题讨论】:
标签: vba excel runtime-error