使用范围数组创建公式以添加每个 sheet 中的单元格
Using an array of ranges to create a formula to add cells from each sheet
我正在尝试将数组设置为一组范围。我想 运行 遍历我的每个 sheet,从左边第 5 个开始,select 右边的单元格,它在列中找到文本 "Total" .然后最后一部分只是在它绕过 sheet 时使用它并创建一个字符串,当将其放在 sheet "Final Budget" 中时,它就像一个公式并总结所有值在那个牢房里。
我收到下标超出范围的 运行 时间错误“9”。我之前使用过 Worksheets 的索引 属性,所以我相信我这样做是正确的,但不确定为什么它无法识别下标。
Application.ScreenUpdating = False
Dim EmpFinal() As Variant
Dim total_1() As Variant
Dim total_2() As Variant
Dim totalAddress() As Variant
iSheets = Sheets.count
For i = 5 To iSheets - 1
Set total_1(i) = Worksheets(i).Range("D:D")
Set total_2(i) = total_1(i).Find("Total")
totalAddress(i) = Cells(total_2(i).Row, total_2(i).Column + 1).Address(0, 0)
Formula = "=SUM("
Formula = Formula & Sheets(i).Name & "!" & totalAddress(i) & ","
Next i
Formula = Left(Formula, Len(Formula) - 1) & ")"
Worksheets("Final Budget").Range("I1").Formula = Formula
Application.ScreenUpdating = True
您不需要数组,因为您将值传递给循环内的字符串:
Application.ScreenUpdating = False
Dim EmpFinal As Variant
Dim total_1 As Range
Dim total_2 As Range
Dim totalAddress As string
Dim iSheets as Long
iSheets = Sheets.count
Formula = "=SUM("
Dim i as Long
For i = 5 To iSheets - 1
Set total_1 = worksheets(i).Range("D:D")
Set total_2 = total_1.Find("Total")
If Not total_2 is Nothing then
totalAddress = worksheets(i).Cells(total_2.Row, total_2.Column + 1).Address(0, 0)
Formula = Formula & "'" & workSheets(i).Name & "'!" & totalAddress & ","
End If
Next i
Formula = Left(Formula, Len(Formula) - 1) & ")"
Worksheets("Final Budget").Range("I1").Formula = Formula
Application.ScreenUpdating = True
我正在尝试将数组设置为一组范围。我想 运行 遍历我的每个 sheet,从左边第 5 个开始,select 右边的单元格,它在列中找到文本 "Total" .然后最后一部分只是在它绕过 sheet 时使用它并创建一个字符串,当将其放在 sheet "Final Budget" 中时,它就像一个公式并总结所有值在那个牢房里。
我收到下标超出范围的 运行 时间错误“9”。我之前使用过 Worksheets 的索引 属性,所以我相信我这样做是正确的,但不确定为什么它无法识别下标。
Application.ScreenUpdating = False
Dim EmpFinal() As Variant
Dim total_1() As Variant
Dim total_2() As Variant
Dim totalAddress() As Variant
iSheets = Sheets.count
For i = 5 To iSheets - 1
Set total_1(i) = Worksheets(i).Range("D:D")
Set total_2(i) = total_1(i).Find("Total")
totalAddress(i) = Cells(total_2(i).Row, total_2(i).Column + 1).Address(0, 0)
Formula = "=SUM("
Formula = Formula & Sheets(i).Name & "!" & totalAddress(i) & ","
Next i
Formula = Left(Formula, Len(Formula) - 1) & ")"
Worksheets("Final Budget").Range("I1").Formula = Formula
Application.ScreenUpdating = True
您不需要数组,因为您将值传递给循环内的字符串:
Application.ScreenUpdating = False
Dim EmpFinal As Variant
Dim total_1 As Range
Dim total_2 As Range
Dim totalAddress As string
Dim iSheets as Long
iSheets = Sheets.count
Formula = "=SUM("
Dim i as Long
For i = 5 To iSheets - 1
Set total_1 = worksheets(i).Range("D:D")
Set total_2 = total_1.Find("Total")
If Not total_2 is Nothing then
totalAddress = worksheets(i).Cells(total_2.Row, total_2.Column + 1).Address(0, 0)
Formula = Formula & "'" & workSheets(i).Name & "'!" & totalAddress & ","
End If
Next i
Formula = Left(Formula, Len(Formula) - 1) & ")"
Worksheets("Final Budget").Range("I1").Formula = Formula
Application.ScreenUpdating = True