debugging.Cells(i – 3,“A”)。Formula =“= Sheet2!A”&i&“”

非专业的VBA试图debugging我的代码。 谁制造这个代码是好的。 这个Sub看起来像这样:

Sub UpdateFormulaCUI() Dim i As Integer i = 6 With Sheets("CUI") Do Until Sheets("Sheet2").Cells(i, "A") = "" .Cells(i - 3, "A").Formula = "=Sheet2!A" & i & "" .Cells(i - 3, "B").Formula = "=IFERROR(VLOOKUP(A" & i - 3 & ",Purchases!A:P,7,FALSE),"""")" .Cells(i - 3, "D").Formula = "=GETPIVOTDATA(""Sum of Inventory"",Sheet2!$A$3,""Inventory" & Chr(10) & "Number"",A" & i - 3 & ")" .Cells(i - 3, "E").Formula = "=GETPIVOTDATA(""Average of Cost $"",Sheet2!$A$3,""Inventory" & Chr(10) & "Number"",A" & i - 3 & ")" .Cells(i - 3, "F").Formula = "=D" & i - 3 & "*E" & i - 3 & "" i = i + 1 Loop End With i = 6 With Sheets("CUI") Do Until Sheets("Sheet2").Cells(i, "A") = "" .Cells(i - 3, "A") = .Cells(i - 3, "A") .Cells(i - 3, "B") = .Cells(i - 3, "B") .Cells(i - 3, "D") = .Cells(i - 3, "D") .Cells(i - 3, "E") = .Cells(i - 3, "E") .Cells(i - 3, "F") = .Cells(i - 3, "F") i = i + 1 Loop End With End Sub 

这篇文章的主题行被停止。 由于我对VBA的知识有限,所以我无法理解"=Sheet2!A" & i & ""是什么意思。
请帮忙!

这一行:

 .Cells(i - 3, "A").Formula = "=Sheet2!A" & i & "" 

指出特定单元格中的公式正被设置为某个string。 variables“i”是一个在循环中改变的数字。 例如,当我= 5,你基本上得到了:

 .Cells(2, "A").Formula = "=Sheet2!A5" 

如果你通过macros并停止它,你会发现你正在引用的单元格中有一个公式,它应该是= Sheet2!A5

如果工作表不存在,那可能会导致您的错误。

当这停止时,它是否给出了一个特定的错误?

在这里添加信息 – 上面的引用是指工作表的名称 ,而不是您在macros编辑器中看到的列表。

所以,如果你是Sheet11(Sheet2),那就是你所引用的那个。 如果您看到Sheet2(销售),那么该表的名称是“销售” – 这就是您在公式中使用的内容。