我从这个宏中得到了一个奇怪的输出。宏应该使用上面的颜色填充空白单元格,创建一个颜色块。结果不是我所期望的,尽管Debug.Print
显示相同ColorIndex
number.
知道这里发生了什么吗?
Option Explicit
Sub Crayon()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim MyCell As Range
For Each MyCell In ws.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
If MyCell.Interior.ColorIndex = -4142 Then
If MyCell.Offset(-1).Interior.ColorIndex <> -4142 Then
MyCell.Interior.ColorIndex = MyCell.Offset(-1).Interior.ColorIndex
End If
End If
Next MyCell
End Sub
我曾经按照宏来生成Column G
在照片上,显示了上述宏生成的每个单元格的颜色索引Crayon
。我不知道如何解释的输出颜色不匹配 with 相同的颜色索引.
Sub Index_Output()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim i
For i = 2 To 17
ws.Range("G" & i) = ws.Range("C" & i).Interior.ColorIndex
Next i
End Sub
附:这是我试图回答的this问题,因为这就是我学习/练习 VBA 的方式:)
颜色的不同是由于其性质ColorIndex
,作为MSDN注释代表调色板中颜色的索引。不同调色板中的颜色将具有相同的索引号,因此使用Color
代替ColorIndex
.
Change ColorIndex
to Color
在最里面的If...End IF
.
Sub Crayon()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim MyCell As Range
For Each MyCell In ws.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
If MyCell.Interior.ColorIndex = xlNone Then
If MyCell.Offset(-1).Interior.ColorIndex <> xlNone Then
MyCell.Interior.Color = MyCell.Offset(-1).Interior.Color
End If
End If
Next MyCell
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)