VBA-为什么向我的循环添加一个集合会给我一个类型不匹配错误?

时间:2018-06-19 15:20:36

标签: excel vba excel-vba

我有一个非常长但功能强大的宏,可以根据可变阈值更改单元格背景颜色。我以为,这是执行此操作的最佳方法,因此有0%的机会,所以我尝试将我的一些变量(用于列号的变量)推入一个集合中并循环遍历它们。我不会让您经历原始版本的数百行之苦,而只是简要说明一下:

For i = 14 To 43 

        If Cells(i, rc) = AMER Then

                If Cells(i, NU_C) <= (Cells(AM_R, NU_C) - pct) Then
                    Cells(i, NU_C).Interior.Color = RGB(242, 220, 219)
                Else

                    If Cells(i, NU_C).Interior.Color = RGB(242, 220, 219) Then
                    Cells(i, NU_C).Interior.ColorIndex = 0
                    End If

                End If

                If Cells(i, FW_C) <= (Cells(AM_R, FW_C) - pct) Then
                    Cells(i, FW_C).Interior.Color = RGB(242, 220, 219)
                Else

                    If Cells(i, FW_C).Interior.Color = RGB(242, 220, 219) Then
                    Cells(i, FW_C).Interior.ColorIndex = 0
                    End If

                End If

那很好,除了我必须在所有18个单独的If部分中进行任何更改时哭泣的眼泪。因此,当我尝试用一​​个集合重写它时,这是同一部分:

For i = 14 To 43

                If Cells(i, rc) = AMER Then

                    For Each v In C

                        If Cells(i, v) <= (Cells(AM_R, v) - pct) Then
                            Cells(i, v).Interior.Color = RGB(242, 220, 219)
                        Else

                            If Cells(i, v).Interior.Color = RGB(242, 220, 219) Then
                            Cells(i, v).Interior.ColorIndex = 0
                            End If

                        End If

                    Next v

我将完整的新代码放在底部,但是无论如何,当我尝试运行它时,我得到一个错误13类型不匹配,突出显示了这一行:

If Cells(i, v) <= (Cells(AM_R, v) - pct) Then

我假设它只是因为第一个而突出显示该错误,并且它是比错别字更根本的错误。我尝试使用this page来查找问题所在,但似乎没有我的错误。列出的将两侧都放入“监视”窗口的方法将单元格(i,v)和(Cells(AM_R,v)-pct)都显示为“ Variant / Integer”类型。

完整的代码,虽然仍然很长,但远不及以前:

Sub Test()

Dim i As Integer
Dim rc As Integer
Dim pct As Range

rc = 3 'column


Set pct = Range("AA6")

'Columns
Dim NU_C As Integer
Dim FW_C As Integer
Dim BI_C As Integer
Dim B_C As Integer
Dim DB_C As Integer
Dim TL_C As Integer

NU_C = 9
FW_C = 12
BI_C = 15
B_C = 18
DB_C = 21
TL_C = 24

'Rows
Dim AM_R As Integer
Dim EM_R As Integer
Dim AS_R As Integer

AM_R = 6
EM_R = 7
AS_R = 8

'Regions
Dim AMER As Range
Dim EMEA As Range
Dim ASIA As Range
Set AMER = Cells(AM_R, rc)
Set EMEA = Cells(EM_R, rc)
Set ASIA = Cells(AS_R, rc)


Application.ScreenUpdating = False

Dim C As Collection

Set C = New Collection
C.Add "NU_C"
C.Add "FW_C"
C.Add "BI_C"
C.Add "B_C"
C.Add "DB_C"
C.Add "TL_C"

Dim v As Variant

        For i = 14 To 43

                If Cells(i, rc) = AMER Then

                    For Each v In C

                        If Cells(i, v) <= (Cells(AM_R, v) - pct) Then
                            Cells(i, v).Interior.Color = RGB(242, 220, 219)
                        Else

                            If Cells(i, v).Interior.Color = RGB(242, 220, 219) Then
                            Cells(i, v).Interior.ColorIndex = 0
                            End If

                        End If

                    Next v

                ElseIf Cells(i, rc) = EMEA Then

                    For Each v In C

                        If Cells(i, v) <= (Cells(EM_R, v) - pct) Then
                            Cells(i, v).Interior.Color = RGB(242, 220, 219)
                        Else

                            If Cells(i, v).Interior.Color = RGB(242, 220, 219) Then
                            Cells(i, v).Interior.ColorIndex = 0
                            End If

                        End If

                    Next v

            ElseIf Cells(i, rc) = ASIA Then

                    For Each v In C

                        If Cells(i, v) <= (-pct) Then
                            Cells(i, v).Interior.Color = RGB(242, 220, 219)
                        Else

                            If Cells(i, v).Interior.Color = RGB(242, 220, 219) Then
                            Cells(i, v).Interior.ColorIndex = 0
                            End If

                        End If

                    Next v

            End If

        Next i


End Sub

有人可以向我解释我所缺少的吗?我真的很感激! 另外,顺便说一句,我在这个网站上问的太多了吗?我不想利用自己的优势,而且我不可能回答问题,因为我不是程序员,也不知道我在做什么:\

2 个答案:

答案 0 :(得分:1)

您要向集合中添加字符串文字,而不是要添加的变量。您应该使用:

Set C = New Collection
C.Add NU_C
C.Add FW_C
C.Add BI_C
C.Add B_C
C.Add DB_C
C.Add TL_C

尽管此时您实际上并不需要变量!您还可以使用数组:

Dim C
C = Array(9, 12, 15, 18, 21, 24)
For each v in C

答案 1 :(得分:0)

您要将字符串添加到集合中,然后在Cells方法中引用它。

将值添加到集合中

Set C = New Collection
C.Add "NU_C"
C.Add "FW_C"
C.Add "BI_C"
C.Add "B_C"
C.Add "DB_C"
C.Add "TL_C"

将实际值添加为

    Set C = New Collection
    C.Add 9
    C.Add 12
    C.Add 15
    C.Add 18
    C.Add 21
    C.Add 24

希望这会有所帮助-谢谢