我正在尝试使用匹配功能创建预算,但我甚至无法达到这一点,因为我尝试创建的动态范围是返回错误:“应用程序定义的或对象定义的错误”。
有问题的范围是 rng
Sub Material()
Dim wSheet As Worksheet
Dim x, dwIndex, offSet, count, upperLeft, bottomRight, r, wall As Integer
Dim rng As Range
x = 0
For Each wSheet In Worksheets
If wSheet.Name = "Drywall Pricing" Then
dwIndex = wSheet.Index - 1
End If
Next wSheet
For i = 1 To dwIndex
If Sheets(i).Range("K1").Value > 0 Then
count = 9
offSet = 41
r = 27
For wall = 1 To count
offSet = (offSet * wall) - (offSet * 1)
upperLeft = (r + 16) + offSet
bottomRight = (r + 27) + offSet
rng = Sheets(i).Range(Cells(upperLeft, 0), Cells(bottomRight, 1))
Next wall
End If
Next i
End Sub
有没有人知道变量 rng 无效?任何帮助都会变得非常棒,因为这太荒谬了。
提前致谢!
答案 0 :(得分:2)
尝试:设置 rng = ..
任何对象分配都需要SET关键字: - )
答案 1 :(得分:2)
您需要先设置范围。
Set rng = Sheets(i).Range(Cells(upperLeft, 0), Cells(bottomRight, 1))
答案 2 :(得分:2)
除了需要SET之外,您的Cells属性是不合格的,因此它们将从ActiveSheet返回一个Range。根据您的代码,我认为您想要:
With Sheet(i)
Set rng = .Range(.Cells(upperLeft, 0), .Cells(bottomRight, 1))
End With
请注意单元格调用之前的句点。
答案 3 :(得分:0)
错误发生时upperLeft
和bottomRight
的价值是多少?
这可以转化为现有范围吗?
答案 4 :(得分:0)
您还错误地在第二个语句中声明了大多数变量。
Dim x,y为整数
仅将y声明为整数,将x声明为变体。
答案 5 :(得分:0)
我不确定CELLS电话是对还是错,但我找到了一种方法让它发挥作用。
以下是我正在使用的内容:
upperLeft = (ref + 16) + (offSet * wall)
bottomRight = (ref + 27) + (offSet * wall)
Set rng = Sheets(i).Range("A" & upperLeft & ":B" & bottomRight)
使用上述公式停止发生错误。
感谢大家的快速回复。我第一次使用stackoverflow,我认为这是我最喜欢的东西。
答案 6 :(得分:0)
另请注意,Match将返回范围引用而不是数值。因此,如果没有匹配,您将会收到对象错误。