我正在尝试创建一个循环,该循环写一个引用其上方一系列单元格的公式。
Dim y1 As Integer
Dim x1 As Integer
Dim x2 As Integer
Dim y2 As Integer
Dim i
x1 = 5
y1 = 10
x2 = 43
For i = 1 To 500
Range(Cells(x2, 4)).Value = "=1-Sum(" & Range(Cells(x1, 4), Cells(y1, 4)) & ")"""
x1 = x1 + 22
y1 = y1 + 22
x2 = x2 + 22
y2 = y2 + 22
Next
因此对于单元格D21,我想说“ = 1-SUM(D5:D10)”,D43“ = 1-sum(D27:D32)”等。
答案 0 :(得分:1)
范围需要两个单元格,一个开始和一个结束(或一个字符串)。
Range(Cells(x2, 4))
应该是
Cells(x2, 4)
也
Range(Cells(x1, 4), Cells(y1, 4))
将要尝试合并为字符串的值数组返回。
您需要返回包含字符串的地址:
Range(Cells(x1, 4), Cells(y1, 4)).Address(0,0)
其他说明:
+ 22
都可以使用i
作为乘法器进行内联放置。Dim y1 As Long
Dim x1 As Long
Dim x2 As Long
Dim i As Long
x1 = 5
y1 = 10
x2 = 43
With Worksheets("Sheet1") 'Change to your sheet
For i = 1 To 500
.Cells((i - 1) * 22 + x2, 4).Value = "=1-Sum(" & .Range(.Cells((i - 1) * 22 + x1, 4), .Cells((i - 1) * 22 + y1, 4)).Address(0,0) & ")"
Next
End With