1

こんにちは私はvbaを初めて使用するので、あまり理解できないかもしれませんが、コードでセルの背景色を変更する必要があるときに、「ランタイムエラー9:添え字が範囲外です」というエラーが発生する理由を説明できません。別の

 Sub CompareWorksheets(ws1 As Worksheet, ws2 As Worksheet)
    Dim r As Long, c As Integer
    Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
    Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
    Dim DiffCount As Long
    Application.ScreenUpdating = False
    With ws1.UsedRange
        lr1 = .Rows.Count
        lc1 = .Columns.Count
    End With
    With ws2.UsedRange
        lr2 = .Rows.Count
        lc2 = .Columns.Count
    End With
    maxR = lr1
    maxC = lc1
    If maxR < lr2 Then maxR = lr2
    If maxC < lc2 Then maxC = lc2
    DiffCount = 0
    For c = 1 To maxC
        For r = 1 To maxR
            cf1 = ""
            cf2 = ""
            On Error Resume Next
            cf1 = ws1.Cells(r, c).FormulaLocal
            cf2 = ws2.Cells(r, c).FormulaLocal
            On Error GoTo 0
            If cf1 <> cf2 Then
                DiffCount = DiffCount + 1
                ws1.Cells(r, c).Activate
                ws1.Cells(r, c).Select
 =============> ws1.Cells(r, c).Interior.ColorIndex = RGB(200, 20, 20) <============
                End If
            Next r
        Next c
        Application.ScreenUpdating = True
    End Sub
4

2 に答える 2

5

Cell.Interior.ColorIndexRGB値ではなく、列挙値です。
可能な値は次のとおりです。

  • xlColorIndexAutomatic これは自動カラーを意味します
  • xlColorIndexNone これは色がないことを意味します

これが、 RGB値に正常に設定できない理由です。

背景色をRGBInterior.Color色に設定するには、代わりにプロパティを使用します。

于 2012-02-20T21:27:29.830 に答える
0

RGB(#、#、#)内で&Hタグ付きのInterior.Colorを使用しても機能することを発見しました。

例:

RGB = 192 0 0 ------- 16進数=#C00000 ------ RGB(&HC0、&H0、&H0)
RGB = 84 130 53 ---- 16進数=#548235 ------ RGB (&H54、&H82、&H35)

Range( "C1")。Interior.Color = RGB(&HC0、&H0、&H0)
Range( "C1")。Interior.Color = RGB(&H54、&H82、&H35)

Cells(1、3).Interior.Color = RGB(&HC0、&H0、&H0)
Cells(1、3).Interior.Color = RGB(&H54、&H82、&H35)

于 2021-03-05T17:41:52.270 に答える