【发布时间】:2020-01-09 03:21:25
【问题描述】:
我在尝试我的代码时看到此错误:
运行时错误“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 方法,但出现了相同的错误。
【问题讨论】:
-
您确定
ActiveWorkbook在执行该行时确实是包含Worksheets(Resultados)的工作簿吗?也许如果您按名称或ThisWorkbook定义工作簿。 -
可能在错误行之前添加
Debug.Print行,用于检查letra1、startDate和month的值。 -
另外,关于
startDate,您已将其声明为整数。这将导致 1989 年 9 月 16 日之后的日期出现问题。将其声明为 Long 或 Double。 -
非常感谢男孩们的快速回复:)。问题是索引没有价值。