在位於兩個不同工作表的兩個範圍內添加單元格 (Adding cells in two ranges which are located at two different sheets)


問題描述

在位於兩個不同工作表的兩個範圍內添加單元格 (Adding cells in two ranges which are located at two different sheets)

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.

‑‑‑‑‑

參考解法

方法 1:

Why not use this at line 7 instead?

 outputRng = outputRng.Value + inputRng.Value

EDIT:

You could do yourself a favor for debugging's sake by declaring a couple of Worksheet objects and a couple of Range objects.

Dim shtIn As Worksheet, shtOut As Worksheet
Dim rngIn As Range, rngOut As Range
Set shtIn = Sheets("Input")
Set shtOut = Sheets("Output")
Set rngIn = shtIn.Range("Input")
Set rngOut = shtOut.Range("Output")

Then use these ranges in your code instead of the repeated Sheets("...").Range("...") constructions.

(by MPDExcellll)

參考文件

  1. Adding cells in two ranges which are located at two different sheets (CC BY‑SA 3.0/4.0)

#excel-2003 #excel #vba






相關問題

使用 VBA 根據 B 列的值重置 A 列中的值 (Reset values in column A based on the value of column B using VBA)

Пераўтварэнне формулы ячэйкі ў тэкст з дапамогай excel vba (Converting a cell's formula to text using excel vba)

刪除行並維護輸入範圍 (Delete Rows & Maintain Input Range)

從 VBA 自動排序中排除文本值 (Exclude Text Value From VBA Autosort)

Tại sao tôi không thể tạo biểu đồ này trong excel (sử dụng powershell) (Why can't I create this chart in excel (using powershell))

如何使用 VBA 從 Excel 中的公式中獲取單元格值? (How do I get the cell value from a formula in Excel using VBA?)

讓 Excel 2003 在 Word 文檔中進行查找並返回出現次數 (Have Excel 2003 do a Find in a Word document and return the number of occurences)

驗證下拉條件 (Validation Drop down on a condition)

在位於兩個不同工作表的兩個範圍內添加單元格 (Adding cells in two ranges which are located at two different sheets)

如何在excel中獲得所需的輸出? (How to get the required output in excel?)

從復雜查詢中獲取數據到 Excel (Getting data from a complex query to excel)

使用文本格式的用戶定義輸入從 excel 中檢索數據 (Retrieving data from excel with user defined input that is in text format)







留言討論