我通过其他方式解决了问题(参见最后)。但我很好奇如何将行范围传递给子。原来的问题...
在以下Excel 2007 VBA代码中,我得到了ByRef参数类型不匹配。 也许从foreach范围中提取的行本身不是范围吗?
Sub colorcells()
For Each bgr In Worksheets("1k sort").Range("AO2:AQ1001").Rows
colorrow bgr
Next bgr
For Each bgr In Worksheets("1k sort").Range("as2:au1001").Rows
colorrow bgr
Next bgr
For Each bgr In Worksheets("1k sort").Range("aw2:ay1001").Rows
colorrow bgr
Next bgr
End Sub
Sub colorrow(ByRef gbrrow As Range)
red = gbrrow.Cells(, 3).Value
blue = gbrrow.Cells(, 2).Value
green = gbrrow.Cells(, 1).Value
gbrrow.Interior.Color = RGB(red, green, blue)
red = (128 + red) Mod 256
blue = (128 + blue) Mod 256
green = (128 + green) Mod 256
gbrrow.Font.Color = RGB(red, green, blue)
End Sub
我正在使用Excel电子表格为Kalles Fraktaler分形程序构建1000个条目调色板表。在对RGB值的每个位片进行排序之后(高位然后是2nd,3rd等),我想通过给单元格背景着色来查看电子表格中的颜色。这使文本不可读,因此我添加了代码,还将字体颜色设置为背景颜色的补充。我不想在下一个循环中每行都有8行相同的代码,所以我想创建一个子例程。嗯...不是那么简单。
作为测试,我删除了子调用,并将每个循环的代码内联到三个代码中。例如。
Sub colorcells()
For Each bgr In Worksheets("1k sort").Range("AO2:AQ1001").Rows
red = bgr.Cells(, 3).Value
blue = bgr.Cells(, 2).Value
green = bgr.Cells(, 1).Value
bgr.Interior.Color = RGB(red, green, blue)
red = (128 + red) Mod 256
blue = (128 + blue) Mod 256
green = (128 + green) Mod 256
bgr.Font.Color = RGB(red, green, blue)
Next bgr
For Each bgr In Worksheets("1k sort").Range("as2:au1001").Rows
red = bgr.Cells(, 3).Value
blue = bgr.Cells(, 2).Value
green = bgr.Cells(, 1).Value
bgr.Interior.Color = RGB(red, green, blue)
red = (128 + red) Mod 256
blue = (128 + blue) Mod 256
green = (128 + green) Mod 256
bgr.Font.Color = RGB(red, green, blue)
Next bgr
For Each bgr In Worksheets("1k sort").Range("aw2:ay1001").Rows
red = bgr.Cells(, 3).Value
blue = bgr.Cells(, 2).Value
green = bgr.Cells(, 1).Value
bgr.Interior.Color = RGB(red, green, blue)
red = (128 + red) Mod 256
blue = (128 + blue) Mod 256
green = (128 + green) Mod 256
bgr.Font.Color = RGB(red, green, blue)
Next bgr
End Sub
那产生了一些怪异。范围及其之间的所有列都变宽了。我的“清除颜色”子例程使范围底部的许多字体颜色保持不变。范围的最后两行将其对齐方式更改为居中。 我认为也许试图设置3000种字体颜色是Excel无法准备的。我现在暂时将字体着色掉。
所以解决方法是传递整个范围,不需要传递每一行的每个循环
Sub colorcells()
With Worksheets("1k sort")
colorrange .Range("AO2:AQ1001")
colorrange .Range("as2:au1001")
colorrange .Range("aw2:ay1001")
End With
End Sub
Sub colorrange(ByRef gbrrng As Range)
For Each gbr In gbrrng.Rows
red = gbr.Cells(, 3).Value
blue = gbr.Cells(, 2).Value
green = gbr.Cells(, 1).Value
gbr.Interior.Color = RGB(red, green, blue)
' red = (128 + red) Mod 256
' blue = (128 + blue) Mod 256
' green = (128 + green) Mod 256
' gbr.Font.Color = RGB(red, green, blue)
Next gbr
End Sub
Sub clearcolors()
With Worksheets("1k sort")
clearrangecolor .Range("AO2:AQ1001")
clearrangecolor .Range("as2:au1001")
clearrangecolor .Range("aw2:ay1001")
End With
End Sub
Sub clearrangecolor(ByRef gbrrng As Range)
gbrrng.Font.Color = xlcolorautomatic
gbrrng.Interior.ColorIndex = xlColorIndexNone
End Sub
只需注释一下并测试字体颜色即可。在大多数情况下,它仍然使范围的最后两行中的大多数保持居中对齐。我将其保留为注释,重点是在我编写Kalles fraktaler调色板文件之前,快速检查颜色排序不会导致某些令人讨厌的条纹。我真的不需要读取rgb值,只需查看颜色即可。
谢谢您的帮助,
Jeremy Thomson
答案 0 :(得分:0)
我认为.rows不会返回范围引用,您可以在首次调用之前在本地窗口中查看bgr,以了解其含义。无论如何,您都应该在使用它之前将其显式调暗为范围。
如果要使用:
对于(ws).range(AO2:AO502).cells中的每个bgr
然后,您可以更改调用的例程以重新引用gbrow以获得其他值,即 设置gbrow = gbrow.resize(1,3)
答案 1 :(得分:0)
colorrow()
将Range对象用作参数。默认情况下,bgr
是Variant数据类型,因为您从未将其声明为Range。这将导致编译器抛出ByRef类型不匹配错误。
Sub colorcells()
Dim bgr As Range
For Each bgr In Worksheets("1k sort").Range("AO2:AQ1001").Rows
colorrow bgr
Next bgr
For Each bgr In Worksheets("1k sort").Range("as2:au1001").Rows
colorrow bgr
Next bgr
For Each bgr In Worksheets("1k sort").Range("aw2:ay1001").Rows
colorrow bgr
Next bgr
End Sub
Sub colorrow(ByRef gbrrow As Range)
red = gbrrow.Cells(, 3).Value
blue = gbrrow.Cells(, 2).Value
green = gbrrow.Cells(, 1).Value
gbrrow.Interior.Color = RGB(red, green, blue)
red = (128 + red) Mod 256
blue = (128 + blue) Mod 256
green = (128 + green) Mod 256
gbrrow.Font.Color = RGB(red, green, blue)
End Sub