Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 计算一个范围,然后给它上色_Excel_Runtime Error_Vba - Fatal编程技术网

Excel 计算一个范围,然后给它上色

Excel 计算一个范围,然后给它上色,excel,runtime-error,vba,Excel,Runtime Error,Vba,我试图运行一个简单的脚本来计算一个范围并给它上色,但是我得到了一个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

我试图运行一个简单的脚本来计算一个范围并给它上色,但是我得到了一个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

首先,删除
textstringA
中试图使用其值的引号

Set rngA = ActiveSheet.Range(textstringA)
Range(textrangeA).Interior.ColorIndex = 10
您没有在任何地方定义
textrangeA
。 接下来,如果
t
<2,此操作将失败。所以你需要考虑到这种可能性

试一试


您将看到它的工作原理。

欢迎来到stackoverflow。请添加与您的问题相关的所有标签;这里,特别是你正在使用的语言缺失了。
    Range(textstringA).Interior.ColorIndex = 10