从调色板中查找颜色索引,以便在值更改时为行着色 VBA
Find colorindex from palette for coloring rows when value changes VBA
代码:
Public Sub HighLightRows()
Dim i As Integer
i = 2
Dim c As Integer
c = 2 'Color 1
'Dim colorIndex As XlColorIndex: colorIndex = Application.Dialogs(xlDialogEditColor).Show(10)
'MsgBox colorIndex
Do While (Cells(i, 1) <> "")
If (Cells(i, 1) <> Cells(i - 1, 1)) Then 'check for different value in cell A (index=1)
If c = 2 Then
c = 24 'color 2
Else
c = 2 'color 1
End If
End If
Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.colorIndex = c
i = i + 1
Loop
End Sub
此代码运行良好,并在列中的值更改时更改颜色。但是代码中指定了颜色。我希望用户select颜色his/her选择。
我用上面的代码得到的输出:
我希望代码做什么:
打开调色板。
用户select一种颜色。
颜色索引被传递给一个变量。
当值改变时,行用白色和颜色交替着色select编辑。
例如。如果用户 select 调色板中的蓝色,行将是蓝色和白色,交替 groups.if 用户 select 调色板中的绿色,行将是绿色和白色,交替组。
我尝试包含此代码:
Dim colorIndex As XlColorIndex: colorIndex = Application.Dialogs(xlDialogEditColor).Show(10)
MsgBox colorIndex
调色板完美打开,但 MsgBox colorIndex
输出 -1。
我似乎无法让它工作。代码有任何变化吗?
Dialogs(xlDialogEditColor)
returns True = -1
如果选择了颜色,False = 0
如果用户按下取消。要获得选定的颜色,请使用 ActiveWorkbook.Colors(10)
,如下例所示。
Option Explicit
Public Sub ColorPaletteDialogBox()
Dim lcolor As Long
If Application.Dialogs(xlDialogEditColor).Show(10) = True Then
'user pressed OK
lcolor = ActiveWorkbook.Colors(10)
ActiveCell.Interior.Color = lcolor
Else
'user pressed Cancel
End If
End Sub
所以对于你的循环,你可以使用类似...
Option Explicit
Public Sub HighLightRows()
Dim c As Integer
c = 2 'Color 1
Dim i As Long 'integer is too small for row counting!
i = 2
If Application.Dialogs(xlDialogEditColor).Show(10) = True Then
Do While (Cells(i, 1) <> "")
If (Cells(i, 1) <> Cells(i - 1, 1)) Then 'check for different value in cell A (index=1)
If c = 2 Then
c = 10 'color 2
Else
c = 2 'color 1
End If
End If
Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.colorIndex = c
i = i + 1
Loop
Else
'user pressed Cancel
End If
End Sub
代码:
Public Sub HighLightRows()
Dim i As Integer
i = 2
Dim c As Integer
c = 2 'Color 1
'Dim colorIndex As XlColorIndex: colorIndex = Application.Dialogs(xlDialogEditColor).Show(10)
'MsgBox colorIndex
Do While (Cells(i, 1) <> "")
If (Cells(i, 1) <> Cells(i - 1, 1)) Then 'check for different value in cell A (index=1)
If c = 2 Then
c = 24 'color 2
Else
c = 2 'color 1
End If
End If
Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.colorIndex = c
i = i + 1
Loop
End Sub
此代码运行良好,并在列中的值更改时更改颜色。但是代码中指定了颜色。我希望用户select颜色his/her选择。
我用上面的代码得到的输出:
我希望代码做什么:
打开调色板。
用户select一种颜色。
颜色索引被传递给一个变量。
当值改变时,行用白色和颜色交替着色select编辑。
例如。如果用户 select 调色板中的蓝色,行将是蓝色和白色,交替 groups.if 用户 select 调色板中的绿色,行将是绿色和白色,交替组。
我尝试包含此代码:
Dim colorIndex As XlColorIndex: colorIndex = Application.Dialogs(xlDialogEditColor).Show(10)
MsgBox colorIndex
调色板完美打开,但 MsgBox colorIndex
输出 -1。
我似乎无法让它工作。代码有任何变化吗?
Dialogs(xlDialogEditColor)
returns True = -1
如果选择了颜色,False = 0
如果用户按下取消。要获得选定的颜色,请使用 ActiveWorkbook.Colors(10)
,如下例所示。
Option Explicit
Public Sub ColorPaletteDialogBox()
Dim lcolor As Long
If Application.Dialogs(xlDialogEditColor).Show(10) = True Then
'user pressed OK
lcolor = ActiveWorkbook.Colors(10)
ActiveCell.Interior.Color = lcolor
Else
'user pressed Cancel
End If
End Sub
所以对于你的循环,你可以使用类似...
Option Explicit
Public Sub HighLightRows()
Dim c As Integer
c = 2 'Color 1
Dim i As Long 'integer is too small for row counting!
i = 2
If Application.Dialogs(xlDialogEditColor).Show(10) = True Then
Do While (Cells(i, 1) <> "")
If (Cells(i, 1) <> Cells(i - 1, 1)) Then 'check for different value in cell A (index=1)
If c = 2 Then
c = 10 'color 2
Else
c = 2 'color 1
End If
End If
Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.colorIndex = c
i = i + 1
Loop
Else
'user pressed Cancel
End If
End Sub