计算范围,然后为其着色

时间:2015-03-16 21:22:50

标签: excel-vba runtime-error vba excel

我正在尝试运行一个简单的脚本来计算范围并为其着色,但我收到了1004错误。

范围计数可以正常显示计数范围。

Sub text()
    Dim t As Integer
    t = Application.WorksheetFunction.CountIf(Range("A:A"), "*")
    Dim textstringA As String
    textstringA = "A2:A" & t
    Range("J1") = textstringA

    'Coloring the counted range does not work. Only 1 active sheet exists in the book. 
    'I've tried several other similar methods shown in the forum, but I keep getting 
    'errors on the same line. I tried using 'select' as well before coloring, but no 
    'good. 

    Dim rngA As Range
    Set rngA = ActiveSheet.Range("textstringA")    `debug_error_here`
    Range("textrangeA").Interior.ColorIndex = 10 
End Sub

1 个答案:

答案 0 :(得分:0)

首先,删除textstringA中的引号,只要您尝试使用其值...

Set rngA = ActiveSheet.Range(textstringA)
Range(textrangeA).Interior.ColorIndex = 10

您无法在任何地方定义textrangeA。 接下来,如果t<所以你需要考虑到这种可能性。

尝试

    Range(textstringA).Interior.ColorIndex = 10

你会发现它有效。