【问题标题】:Run-time error '1004': Application-defined or object-defined error when trying to define the Range运行时错误“1004”:尝试定义范围时应用程序定义或对象定义的错误
【发布时间】: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 行,用于检查letra1startDatemonth 的值。
  • 另外,关于startDate,您已将其声明为整数。这将导致 1989 年 9 月 16 日之后的日期出现问题。将其声明为 Long 或 Double。
  • 非常感谢男孩们的快速回复:)。问题是索引没有价值。

标签: excel vba


【解决方案1】:

这可能与“索引”超出范围有关。在这种情况下,“letra1”和“letra2”将是Null,所以resultado.Cells(1,letra1)会导致运行时错误1004。我对此进行了测试,发现它是正确的。

为避免这种情况,您可以再添加一个 If 以作为一个包罗万象:

If index < 1 Or index > 5 Then
    MsgBox "Invalid index"
    Exit Sub
End If

...或者为了简化整个选择过程,考虑将所有If 语句替换为Select Case

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)

【讨论】:

  • 非常感谢:)。你是对的,索引没有价值。
猜你喜欢
  • 2013-08-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2016-01-14
相关资源
最近更新 更多