在位于两个不同工作表的两个范围中添加单元格
我正在尝试对两个范围中相应的单元格值求和,并将其中一个范围中的值替换为 sum 。请参阅下面的代码片段:
For i = 1 To daterow.Rows.count - 1
If (outinter - diff) > 0.5 Then
diff = diff + DateDiff("n", Indt(i - 1), Indt(i))
Y = Sheets("output").Cells(Rows.count, 1).End(xlUp).row + 1
Set inputRng = Sheets("Input").Range(Sheets("Input").Cells(i, 7), _
Sheets("input").Cells(i, ngag + 6))
Set outputRng = Sheets("Output").Range(Sheets("Output").Cells(Y, 6), _
Sheets("Output").Cells(Y, ngag + 5))
outputRng.value = Evaluate(outputRng.Address & "+" & inputRng.Address)
Else
'Some Code here
End If
Next
因此,我在第 5 行和第 6 行中设置了两个范围(inputRng 和 outputRng)。 inputRng 来自 Sheets("Input"),outputRng 来自 Sheets("output")。当我运行代码时,“Evaluate”函数(第 7 行)对范围进行求和,但问题是:
两个范围都源自同一张工作表,并且由于在此计算过程中我处于工作表(“输入”)中,因此两个范围的值inputRng 和outputRng 源自sheets("input")。当我按如下方式重写第 7 行时:
sheets("output").outputRng.value = Evaluate(sheets("output").outputRng.Address _
& "+" & sheets("input").inputRng.Address)
我收到一条错误消息“对象不支持此属性或方法”。有没有其他方法可以强制代码从我的表格(“输出”)读取outputRng?
我可以循环遍历每个单元格并对值求和,但运行代码需要很长时间,因为该宏必须处理大量数据。
任何想法将不胜感激。感谢您的关注。
MPD
解决方案
糟糕,服务器不让我回答问题,所以,我将解决方案发布在这里:
感谢大家的输入。事实上,Excelll 的最后一条评论引起了我的注意!所以,我是这样工作的:
Dim value() As Variant
Dim shtIn As Worksheet, shtOut As Worksheet
Set shtIn = Sheets("Input")
Set shtOut = Sheets("Output")
Redim Value(daterow.Rows.count - 1)
For i = 1 To daterow.Rows.count - 1
If (outinter - diff) > 0.5 Then
diff = diff + DateDiff("n", Indt(i - 1), Indt(i))
Y = shtOut.Cells(Rows.count, 1).End(xlUp).row + 1
Set inputrng = shtIn.Range(shtIn.Cells(i, 7), shtIn.Cells(i, ngag + 6))
Set outputRng = shtOut.Range(shtOut.Cells(Y, 6), shtOut.Cells(Y, ngag + 5))
value = Evaluate("Output!" & outputRng.Address & "+" _
& "Input!" & inputrng.Address)
shtOut.Range(shtOut.Cells(Y, 6), shtOut.Cells(Y, ngag + 5)) = value
Else
'Some Code here
End If
Next
所以,我没有将总和写到另一张纸的范围中,而是将其存储到一个数组中,然后将数组传输到输出范围中,这样我就可以在不循环的情况下完成。
I am trying to sum corresponding cell values in two ranges and replace the values in one of the ranges with the sum . See my code snippet below:
For i = 1 To daterow.Rows.count - 1
If (outinter - diff) > 0.5 Then
diff = diff + DateDiff("n", Indt(i - 1), Indt(i))
Y = Sheets("output").Cells(Rows.count, 1).End(xlUp).row + 1
Set inputRng = Sheets("Input").Range(Sheets("Input").Cells(i, 7), _
Sheets("input").Cells(i, ngag + 6))
Set outputRng = Sheets("Output").Range(Sheets("Output").Cells(Y, 6), _
Sheets("Output").Cells(Y, ngag + 5))
outputRng.value = Evaluate(outputRng.Address & "+" & inputRng.Address)
Else
'Some Code here
End If
Next
So, I set up two ranges (inputRng and outputRng) in lines 5 and 6. inputRng is from sheets("Input") and outputRng is from Sheets("output"). When I run the code, the "Evaluate" function (line 7) sums the ranges but here is the problem:
Both ranges are derived from the same sheet and since I am in sheets("input") during this computation, values for both inputRng and outputRng are derived from sheets("input"). When I rewrite line 7 as follows:
sheets("output").outputRng.value = Evaluate(sheets("output").outputRng.Address _
& "+" & sheets("input").inputRng.Address)
I get an error message "Object does not support this property or method". Is there any other way to force the code to read the outputRng from my Sheets("output")?
I could loop through each cell and sum the values, but it would take a long time to run the code as this macro has to handle a lot of data.
Any thought will be greatly appreciated. Thanks for looking into it.
MPD
Solution
Oops, the server did not let me answer the question, So, I am posting the solution here instead:
Thanks everyone for your input. In fact Excellll's last comment clicked my mind! So, here is how i got it working:
Dim value() As Variant
Dim shtIn As Worksheet, shtOut As Worksheet
Set shtIn = Sheets("Input")
Set shtOut = Sheets("Output")
Redim Value(daterow.Rows.count - 1)
For i = 1 To daterow.Rows.count - 1
If (outinter - diff) > 0.5 Then
diff = diff + DateDiff("n", Indt(i - 1), Indt(i))
Y = shtOut.Cells(Rows.count, 1).End(xlUp).row + 1
Set inputrng = shtIn.Range(shtIn.Cells(i, 7), shtIn.Cells(i, ngag + 6))
Set outputRng = shtOut.Range(shtOut.Cells(Y, 6), shtOut.Cells(Y, ngag + 5))
value = Evaluate("Output!" & outputRng.Address & "+" _
& "Input!" & inputrng.Address)
shtOut.Range(shtOut.Cells(Y, 6), shtOut.Cells(Y, ngag + 5)) = value
Else
'Some Code here
End If
Next
So, instead of writing out the sum into a range in another sheet, I stored that into an array and later transferred the array into the Output Range which I could do without looping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不在第 7 行使用它呢?
编辑:
您可以通过声明几个 Worksheet 对象和几个 Range 对象来帮助自己进行调试。
然后在代码中使用这些范围,而不是重复的
Sheets("...").Range("...")
结构。Why not use this at line 7 instead?
EDIT:
You could do yourself a favor for debugging's sake by declaring a couple of Worksheet objects and a couple of Range objects.
Then use these ranges in your code instead of the repeated
Sheets("...").Range("...")
constructions.