正确的公式给出“运行时错误”1004“:应用程序定义或对象定义”错误时运行子

我想粘贴公式的B列之间的延迟。 公式应该只粘贴公式,如果单元格留给它(在B1的情况下这是A1)不是空的,如下所示:

在这里输入图像说明

我有下面的VBA粘贴延迟1秒的公式。

Option Explicit Sub RetrieveDataDelay() 'paste formulas into cells, then calculate, then delay rinse repeat '========================= 'Declare Variables '========================= Dim i As Long 'used to loop Dim rowCount As Long Dim LastRow As Long 'used to find the last row '========================= 'Setup for Speed '========================= Application.ScreenUpdating = False Application.Calculation = xlCalculationManual '========================= 'Define Variables '========================= LastRow = Worksheets(ActiveSheet.Name).Cells(Rows.Count, 1).End(xlUp).Row 'This will find the last used row in column A, change the number 1 to whatever column number you want rowCount = 1 ' set to how many rows you want to do at a time '========================= 'Do Work '========================= For i = 1 To LastRow Step rowCount Range("B" & i, "B" & WorksheetFunction.Min(i + rowCount - 1, LastRow)).Formula = "'=IF(ISBLANK(A" & i & ");" & """" & """" & ";Dump(Volumes(A" & i & ";2528;1010;TRUE;" & "Volume" & ";TRUE)))" 'set the formula to whatever it needs to be Calculate Application.Wait (Now + TimeValue("00:00:01")) 'this delays 1 second Next i '========================= 'Setup for Speed - Reverse '========================= Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub 

错误发生在这个部分

 Range("B" & i, "B" & WorksheetFunction.Min(i + rowCount - 1, LastRow)).Formula = "=IF(ISBLANK(A" & i & ");" & """" & """" & ";Dump(Volumes(A" & i & ";2528;1010;TRUE;" & "Volume" & ";TRUE)))" 

这个错误有一些公式是应该的,这是它不接受的。 这是一个与Excel扩展一起使用的自定义公式。 我知道这个公式是有效的,因为我在前面加了一个单引号:

 Range("B" & i, "B" & WorksheetFunction.Min(i + rowCount - 1, LastRow)).Formula = "'=IF(ISBLANK(A" & i & ");" & """" & """" & ";Dump(Volumes(A" & i & ";2528;1010;TRUE;" & "Volume" & ";TRUE)))" 

以便从字面上正式粘贴。 如果我然后从公式中删除引号,则公式起作用。 所以问题仍然是为什么它不接受在VBA这个具体的公式。

使用Excel 2013。

Range.Formula总是需要en_us格式的公式符号,与当前的区域设置无关。 这意味着英语的function和逗号作为公式参数之间的分隔符而不是分号。

所以

 .Formula = "=IF(ISBLANK(A" & i & ")," & """" & """" & ",Dump(Volumes(A" & i & ",2528,1010,TRUE," & "Volume" & ",TRUE)))" 

在设置了Range.Formula ,区域设置仍然在工作表中激活。 所以在表格中,如果这样设置,公式将用分号分隔。

顺便说一句:双引号的复杂的string符号是不必要的。

 .Formula = "=IF(ISBLANK(A" & i & "),"""",Dump(Volumes(A" & i & ",2528,1010,TRUE," & "Volume" & ",TRUE)))"