【问题标题】:Cell colour condition loop.Object required error单元格颜色条件循环。需要对象错误
【发布时间】:2014-12-20 06:10:47
【问题描述】:

我正在尝试创建两个数组:一个具有橙色单元格的行号,另一个具有蓝色单元格的行号。我一直在尝试调试这段代码,但它给了我错误:“编译错误:需要对象”;同时以黄色突出显示函数的第一行:“Function ArrayOrangeBlue()”。我是 VBA 的新手,我很确定我在语法中遗漏了一些东西。

请问您有什么意见吗?

Sub CommandButton1_Clicked()
    ArrayOrangeBlue
End Sub

Function ArrayOrangeBlue()

Dim i As Integer    'row number'
Dim j As Integer    'orange counter'
Dim k As Integer    'blue counter'

Dim l As Integer
Dim m As Integer

Dim blue(1 To 1000) As Double
Dim orange(1 To 1000) As Double

'Starting Row'
Set i = 10

'Initialize orange and blue counters to 1'
Set j = 1
Set k = 1

Set l = 10
Set m = 10


'Loop until Row 1000'
Do While i <= 1000

'Dim cell As Range

'Set cell = ActiveSheet.Cells(i, 1)

    'If cell colour is Orange- note absolute row number (i) in array: orange'
    If Cells(i, 1).Interior.Color = 9420794 Then

        orange(j) = i
        Sheets("Detail analysis").Cells(l, 15) = i
        j = j + 1
        l = l + 1
        'MsgBox ("This one is Orange")

    Else

        'If cell colour is Blue- note absolute row number (i) in array: blue'
        If Cells(i, 1).Interior.Color = 13995347 Then

        blue(k) = i
        Sheets("Detail analysis").Cells(m, 16) = i
        k = k + 1
        m = m + 1
        'MsgBox ("This one is Blue")

        End If

    End If

    i = i + 1
Loop


End Function

【问题讨论】:

  • 从包含变量赋值行的行中省略set,例如set j=1

标签: vba colors conditional-statements do-while


【解决方案1】:

所以最后我设法解决了我的代码以查找并记下具有某些颜色的单元格。

这里的代码将橙色单元格的行号存储在数组 orange() 和数组 Arr() 的第 1 列中,并将蓝色单元格的行号存储在数组 blue() 和数组 Arr() 的第 2 列中。

我在初始化变量时删除了“设置”命令。

但是,更重要的是,我需要在每个 If 语句中指定工作表。

我希望这对其他人有帮助。

'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ArrayOrangeBlue Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'

Dim i As Integer    ' row number '
Dim j As Integer    ' orange counter '
Dim k As Integer    ' blue counter '

Dim l As Integer
Dim m As Integer

Dim blue(1 To 50) As Double
Dim orange(1 To 50) As Double
Dim green As Double


' Starting Row '

i = 10

' Initialize orange and blue counters to 1 '

j = 1
k = 1



l = 10
m = 10
Dim Arr(100, 2) As Double

' Loop until Row 1000 '

Do While i <= 1000


''''' If cell colour is Orange- note absolute row number (i) in array: orange '

If Sheets("Detail analysis").Cells(i, 1).Interior.Color = 9420794 Then

    orange(j) = i
    Arr(j, 1) = i


    j = j + 1

Else

''''''''' If cell colour is Blue- note absolute row number (i) in array: blue '

    If Sheets("Detail analysis").Cells(i, 1).Interior.Color = 13995347 Then

    blue(k) = i
    Arr(k, 2) = i


    k = k + 1

    Else

''''''''''''' If cell colour is Gren- store the absolute row number (i) in: green '

        If Sheets("Detail analysis").Cells(i, 1).Interior.Color = 5296274 Then

        Arr(j, 1) = i
        green = i

        End If
    End If
End If

i = i + 1
Loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-21
    • 2020-11-25
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多