运行时错误:424:当我尝试达到范围并使用范围数据时所需的对象

时间:2017-12-26 20:53:16

标签: vba range

我试图以与旧excel相同的方式突出显示新的excel并且数据很大。所以我保存了范围内的数据并尝试查找,计算范围内的函数。但是它一直显示“object not find error”因为我很好地定义了范围对象,所以我真的不明白。这是我的代码的一部分。我在定义RangSe1对象之后尝试通过“RangSe1(1,1).Activate”进行调试,它甚至从这里给出了424错误。我真的很困惑。

 Sub Morningsmall()

 Dim strfile As String
 Dim iLastrow, iColor, iFind, iLastrow1, iLastrow2, iLastrow3, iLastrow4,   iRow As Long
 Dim RangSe1, RangSo1, RangSe2, RangSo2, RangS As Range


 Dim wbLastday, wbToday As Workbook
 Dim wsSettle1, wsSettle2, wsSophis1, wsSophis2 As Worksheet

    With Application

        .ScreenUpdating = False
        .EnableEvents = False
        .Calculate
        .Calculation = xlCalculationManual
        .DisplayStatusBar = False
    End With


  'Open yesterday's file
   MsgBox "Open Yesterday's Settlement Report"
   strfile = Application.GetOpenFilename
   If strfile <> "False" Then Workbooks.Open strfile

Set wbLastday = ActiveWorkbook
Set wsSettle1 = wbLastday.Sheets("SettlementReport")
Set wsSophis1 = wbLastday.Sheets("Sophis")

iLastrow1 = wsSettle1.Cells(wsSettle1.Rows.Count, 1).End(xlUp).Row
iLastrow2 = wsSophis1.Cells(wsSophis1.Rows.Count, 1).End(xlUp).Row

RangSe1 = wsSettle1.Range("A1:AQ" & iLastrow1)
RangSo1 = wsSophis1.Range("A1:AJ" & iLastrow2)


RangSe1(1, 1).Activate

...
...
...


For i = 2 To iLastrow3
    iFind = RangSe2(i, 1)
    'a = Application.WorksheetFunction.CountIf(Rang, iFind)
    If Application.WorksheetFunction.CountIf(wsSettle1, iFind) > 0 Then

   'range1.Find("test id", LookIn:=xlValues)
        If RangSe1(wsSettle1.Cells.Find(what:=iFind).Row, 6) = RangSe2(i,           6)     Then
       iColor = RangSe1.Find(what:=iFind).Interior.Color
           If iColor <> 16777215 Then
                 wsSettle2.Rows(i).Interior.Color = iColor
           End If
       End If
 End If

...
...
...

1 个答案:

答案 0 :(得分:1)

你的话说

Dim RangSe1
'...
RangSe1 = wsSettle1.Range("A1:AQ" & iLastrow1)

相当于

Dim RangSe1 As Variant
'...
RangSe1 = wsSettle1.Range("A1:AQ" & iLastrow1).Value

将创建一个Variant数组,其大小为1 To iLastrow1, 1 To 43。您不能在数组的Activate位置使用(1, 1)方法,因为数组不是对象,因此没有方法或属性。

您有两个主要错误导致您的代码无法达到预期效果:

1)您没有正确定义变量,因为:

Dim RangSe1, RangSo1, RangSe2, RangSo2, RangS As Range

相当于:

Dim RangSe1 As Variant, RangSo1 As Variant, RangSe2 As Variant, RangSo2 As Variant, RangS As Range

您应该使用:

Dim RangSe1 As Range, RangSo1 As Range, RangSe2 As Range, RangSo2 As Range, RangS As Range

2)在为Set对象分配引用时,您没有使用Range关键字,例如,

RangSe1 = wsSettle1.Range("A1:AQ" & iLastrow1)

应该是

Set RangSe1 = wsSettle1.Range("A1:AQ" & iLastrow1)
相关问题