[英]Run-time error 1004 Application-defined or Object-defined error when setting Formula property
[英]Run-time error '1004': Application-defined or object-defined error when trying to define the Range
當我嘗試我的代碼時看到這個錯誤:
運行時錯誤“1004”:應用程序定義或對象定義錯誤
我的代碼:
Sub search(date1, month, sheet, index)
Dim cell As Range
Dim startDate As Integer
Dim finalIndex As Integer
Dim letra1 As Single
Dim letra2 As Single
Dim textoPlano As Integer
Dim svcPoliza As Integer
Dim svcMarcas As Integer
Dim svcDptos As Integer
Dim svcCotizacionesCme As Integer
Dim svcAniosVehiculo As Integer
Dim svcRiesgoVigente As Integer
Dim svcLineasVehiculos As Integer
Dim svcLeeLocalidades As Integer
Dim hoja As Worksheet
Dim ultimaFila As Long
Dim resultado As Worksheet
Set resultado = ActiveWorkbook.Worksheets("Resultados")
Set hoja = ActiveWorkbook.Worksheets("Sheet" & sheet)
If sheet = "Consolidado" Then
Set hoja = ActiveWorkbook.Worksheets("Consolidado")
End If
ultimaFila = hoja.Cells(hoja.Rows.Count, "G").End(xlUp).Row
For Each cell In Range("G3:G" & ultimaFila)
If InStr(cell.Value, "enviarCorreoTextoPlano") > 0 Then
textoPlano = textoPlano + 1
End If
If InStr(cell.Value, "svcPolizaRecienteVehiculo") > 0 Then
svcPoliza = svcPoliza + 1
End If
If InStr(cell.Value, "svcMarcasVehiculos") > 0 Then
svcMarcas = svcMarcas + 1
End If
If InStr(cell.Value, "svcLeeDptos") > 0 Then
svcDptos = svcDptos + 1
End If
If InStr(cell.Value, "svcLeeCotizacionesCme") > 0 Then
svcCotizacionesCme = svcCotizacionesCme + 1
End If
If InStr(cell.Value, "svcLeeAniosVehiculo") > 0 Then
svcAniosVehiculo = svcAniosVehiculo + 1
End If
If InStr(cell.Value, "svcRiesgoVigenteVehiculo") > 0 Then
svcRiesgoVigente = svcRiesgoVigente + 1
End If
If InStr(cell.Value, "svcLineasVehiculos") > 0 Then
svcLineasVehiculos = svcLineasVehiculos + 1
End If
If InStr(cell.Value, "svcLeeLocalidades") > 0 Then
svcLeeLocalidades = svcLeeLocalidades + 1
End If
Next cell
If index = 1 Then
letra1 = 1
letra2 = 2
startDate = date1
End If
If index = 2 Then
letra1 = 3
letra2 = 4
startDate = date1 + 1
End If
If index = 3 Then
letra1 = 5
letra2 = 6
startDate = date1 + 2
End If
If index = 4 Then
letra1 = 7
letra2 = 8
startDate = date1 + 3
End If
If index = 5 Then
letra1 = 9
letra2 = 10
startDate = date1 + 4
End If
resultado.Cells(1, letra1).Value = "Dia " & startDate & "-" & month
resultado.Cells(2, letra1).Value = "enviarCorreoTextoPlano"
resultado.Cells(3, letra1).Value = "svcPolizaRecienteVehiculo"
resultado.Cells(4, letra1).Value = "svcMarcasVehiculos"
resultado.Cells(5, letra1).Value = "svcLeeDptos"
resultado.Cells(6, letra1).Value = "svcLeeCotizacionesCme"
resultado.Cells(7, letra1).Value = "svcLeeAniosVehiculo"
resultado.Cells(8, letra1).Value = "svcRiesgoVigenteVehiculo"
resultado.Cells(9, letra1).Value = "svcLineasVehiculos"
resultado.Cells(10, letra1).Value = "svcLeeLocalidades"
resultado.Cells(2, letra2).Value = textoPlano
resultado.Cells(3, letra2).Value = svcPoliza
resultado.Cells(4, letra2).Value = svcMarcas
resultado.Cells(5, letra2).Value = svcDptos
resultado.Cells(6, letra2).Value = svcCotizacionesCme
resultado.Cells(7, letra2).Value = svcAniosVehiculo
resultado.Cells(8, letra2).Value = svcRiesgoVigente
resultado.Cells(9, letra2).Value = svcLineasVehiculos
resultado.Cells(10, letra2).Value = svcLeeLocalidades`enter code here`
End Sub
調試器顯示這一行:
resultado.Cells(1, letra1).Value = "Dia " & startDate & "-" & month
當我使用 Range 而不是 Cells 方法來定義單元格時,我第一次檢測到此錯誤。 然后我將其更改為 Cells 方法,但出現了相同的錯誤。
這可能與“索引”超出范圍有關。 在這種情況下,“letra1”和“letra2”將為Null
,因此resultado.Cells(1,letra1)
將導致運行時錯誤 1004。我對此進行了測試,發現它是正確的。
為避免這種情況,您可以再添加一個If
作為包羅萬象:
If index < 1 Or index > 5 Then
MsgBox "Invalid index"
Exit Sub
End If
...或者為了簡化整個選擇過程,請考慮用Select Case
替換所有If
語句:
Select Case index
Case 1
letra1 = 1
letra2 = 2
startDate = date1
Case 2
letra1 = 3
letra2 = 4
startDate = date1 + 1
Case 3
letra1 = 5
letra2 = 6
startDate = date1 + 2
Case 4
letra1 = 7
letra2 = 8
startDate = date1 + 3
Case 5
letra1 = 9
letra2 = 10
startDate = date1 + 4
Case Else
MsgBox "Invalid index"
Exit Sub
End Select
編輯
一個更簡單的解決方案可能是只計算“letra1”、“letra2”和“startdate”而沒有所有的if
語句,只需首先驗證“index”不小於 1:
If index < 1 Then
MsgBox "Invalid index"
GoTo DoneHere
End If
letra1 = index + (index - 1)
letra2 = letra1 + 1
startDate = date1 + (index - 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.