Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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/0/vba/14.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 更改为单元格时产生范围运行时错误1004_Excel_Vba_Range_Runtime Error - Fatal编程技术网

Excel 更改为单元格时产生范围运行时错误1004

Excel 更改为单元格时产生范围运行时错误1004,excel,vba,range,runtime-error,Excel,Vba,Range,Runtime Error,当我将范围从字母更改为单元格时,下面的部分开始产生运行时错误1004: 随信附上: Application.SumIf(Sheets(4).Range("E:P"), _ Sheets(6).Cells(i, 1).Value2, _ Sheets(4).Range("K:K")) 无信: Application.SumIf( _ Sheets(4).Range(Cells(1, 5), Cells(intLstRowA, 16)), _ Sheets(6).Cells(i,

当我将范围从字母更改为单元格时,下面的部分开始产生运行时错误1004:

随信附上:

Application.SumIf(Sheets(4).Range("E:P"), _
Sheets(6).Cells(i, 1).Value2, _
Sheets(4).Range("K:K"))
无信:

Application.SumIf( _ 
    Sheets(4).Range(Cells(1, 5), Cells(intLstRowA, 16)), _
    Sheets(6).Cells(i, 1).Value2, _
    Sheets(4).Range(Cells(1, 11), Cells(intLstRowA, 11)))
它是更大循环的一部分:

intLstRowA = Sheets(4).Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To 9
    Sheets(6).Cells(i, 2) = _
        Format( _
            Application.SumIf( _
                Sheets(4).Range(Cells(1, 5), Cells(intLstRowA, 16)), _
                Sheets(6).Cells(i, 1).Value2, _
                Sheets(4).Range(Cells(1, 11), Cells(intLstRowA, 11))) _ 
                / _
            Application.SumIf(Sheets(4).Range("E:P"), _
                Sheets(6).Cells(i, 1).Value2, _
                Sheets(4).Range("P:P")) * 0.01, _
           "Percent")
Next i
我从不使用.Select、.Active等,我无法找出导致错误的原因。你能帮我检查一下这是否正常吗?

Sheets(4)。范围(单元格(1,5),单元格(intLstRowA,16))
仅当
Sheets(4)
ActiveSheet
时才有效

这是因为
单元格(1,5)
没有说明是指谁的单元格。所以活性纸的细胞是可信的

Sheets(4).范围(Sheets(4).单元格(1,5),Sheets(4).单元格(intLstRowA,16))
可以工作

但最好是使用

With ActiveWorkbook.Sheets(4)
...
... .Range(.Cells(1, 5), .Cells(intLstRowA, 16))...
...
End With

这可能是因为您没有正确地限定
单元格
属性调用。如果您不符合条件,它将假定您引用的是当前活动的工作表

Application.SumIf( _ 
    Sheets(4).Range(Cells(1, 5), Cells(intLstRowA, 16)), _
    Sheets(6).Cells(i, 1).Value2, _
    Sheets(4).Range(Cells(1, 11), Cells(intLstRowA, 11)))
'                   ^^^^^         ^^^^^
这些单元格在哪张纸上?明确地说:

'                   vvvvvvvvvvvvvvv        vvvvvvvvvvvvvvv
Application.SumIf( _ 
    Sheets(4).Range(Sheets(4).Cells(1, 5), Sheets(4).Cells(intLstRowA, 16)), _
    Sheets(6).Cells(i, 1).Value2, _
    Sheets(4).Range(Sheets(4).Cells(1, 11), Sheets(4).Cells(intLstRowA, 11)))
'                   ^^^^^^^^^^^^^^^         ^^^^^^^^^^^^^^^

如果与
工作表(4)
不同的工作表恰好处于活动状态,前一种情况会给您一个错误,因为它会在一张工作表中定义一个范围,其角由另一张工作表上的单元格定义,这显然是无意义的。

JFC稍早回复,所以我将他的回答标记为回复。再次感谢您的建议,将有相当多的表(4)。不加它就加!