Excel不断抛出我:
运行时错误1004:应用程序定义或对象定义的错误
每次我尝试运行以下代码。我不太确定自己在做什么错(真的很感谢任何建议)。
我定义skumaestro
范围的方式是否有问题?
谢谢
Sub getskus ()
lastskurow = .Cells(.Rows.count, "M").End(xlUp).Row
lasthandlerow = .Cells(.Rows.count, "B").End(xlUp).Row
With ThisWorkbook.Worksheets(2)
Dim skumaestro As Range
Set skumaestro = Range(Cells(lasthandlerow, 13), Cells(lastskurow, 13))
End With
For start = 1 To count
Range("C" & Rows.count).End(xlUp).Offset(1).Select
ActiveCell.FormulaR1C1 = "=INDEX(" & barcodes.Address(True, True,
xlA1, True) & ",MATCH(" & skumaestro(1).Address(False, True, xlA1,
False) & "," & skucodes.Address(True, True, xlA1, True) & ",0))"
ActiveCell.Value2 = ActiveCell.Value2
Next
End Sub
答案 0 :(得分:0)
您没有正确使用With ... End With块。请注意.
,它允许.Range和.Cells继承父级工作表。
With ThisWorkbook.Worksheets(2)
Dim skumaestro As Range
Set skumaestro = .Range(.Cells(lasthandlerow, 13), .Cells(lastskurow, 13))
End With
您的公式使用xlA1地址尝试构建FormulaR1C1公式。没有定义的父工作表。 条形码和 skucodes 未定义。您没有使用 start 来循环浏览任何内容。
我想你想要类似的东西,
For start = 1 To count
with Range("C" & Rows.count).End(xlUp).Offset(1) 'what parent worksheet?
.FormulaR1C1 = "=INDEX(" & barcodes.Address(True, True,
xlR1C1, True) & ",MATCH(" & skumaestro(start).Address(False, True, xlR1C1,
False) & "," & skucodes.Address(True, True, xlR1C1, True) & ",0))"
.Value = .Value2
end with
Next start
您在公式中没有为 skumaestro 使用外部地址,因此可以合理地认为该公式适用于上面使用的同一工作表。
Dim skumaestro As Range, addr1 as string, addr2 as string
With ThisWorkbook.Worksheets(2)
Set skumaestro = .Range(.Cells(lasthandlerow, 13), .Cells(lastskurow, 13))
addr1 = barcodes.Address(True, True, xlR1C1, external:=True)
addr2 = skucodes.Address(True, True, xlR1C1, external:=True)
with .Range("C" & Rows.count).End(xlUp).Offset(1).resize(count, 1)
.FormulaR1C1 = "=INDEX(" & addr1 & ",MATCH(" & skumaestro(1).Address(False, True, xlR1C1, False) & "," & addr2 & ",0))"
.Value = .Value2
end with
End With