我的excel VBA中的语法错误 - 特别是:R1C1形式的工作表函数

时间:2015-08-10 10:38:07

标签: excel vba excel-vba

我遇到了一个VBA脚本的问题,我正在编写这个脚本,以便对我工作更容易执行的业务进行订单簿移动的月末分析。

(我是一个新手,并且已经开始使用Walkenbach先生的VBA for Dummies来学习VBA,只是为了实现我现在坚持的这项任务!)

这个VBA脚本是我编写的一套其他VBA脚本的一部分,这些脚本是为了让任务更容易 - 这是我唯一坚持的脚本。

我之前使用excel执行此分析,只是复制/粘贴公式,然后每个月将不正确的引用更改为不同的工作簿 - 我使用这些工作簿中的公式作为我的VBA脚本的基础,所以我知道它们可以工作在excel中。我怀疑有更容易/更简单的方法来达到同样的目的,但这是我能力的极限!

语法错误发生在**下面的工作表函数中:

  Sub Despatches_Matrix()
 '
 ' Despatches_Matrix Macro
 '
 Dim LastRow As Long, n As Long, LastCol As Long
 Dim RowVar As Integer
 Dim ColVar As Integer

Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Despatches"
Sheets("Difference").Select
Cells.Select
Selection.Copy
Sheets("Despatches").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
Rows("3:3").Select
Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Despatches").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Despatches").Sort.SortFields.Add Key:=Range( _
    "C3:C1000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
With ActiveWorkbook.Worksheets("Despatches").Sort
    .SetRange Range("A3:Bz1000")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

LastRow = Range("c1000").End(xlUp).Row
For n = LastRow To 3 Step -1
If Cells(n, 3).Value = 0 Then Cells(n, 3).EntireRow.Delete

Next n

LastCol = Range("Bz2").End(xlToLeft).Column - 7

'MsgBox (LastCol)

RowVar = 3

Do While Range("A" & RowVar) <> Empty

    Range("D" & RowVar).Formula = "=ROUND(IF(ISNA(VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$B$1000,2,FALSE)),0,IF(AND(VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$B$1000,2,FALSE)>0,VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$B$41000,2,FALSE)>=-C" & RowVar & "),C" & RowVar & ",IF(AND(VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$B$1000,2,FALSE)>0,VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$B$1000,2,FALSE)<-C" & RowVar & "),-VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$B$1000,2,FALSE),(0)))),0)"

    Range("E" & RowVar).Formula = "=ROUND(IF(ISNA(VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$B$1000,3,FALSE)),C" & RowVar & ",IF(AND(C" & RowVar & "<D" & RowVar & ",VLOOKUP(Despatches!A" & RowVar & ",OpeningOrderBookPivot!$A$3:$C$1000,3,FALSE)>0,VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$C$1000,3,FALSE)>=-(Despatches!C" & RowVar & "-Despatches!D" & RowVar & ")),C" & RowVar & "-D" & RowVar & ",IF(AND(C" & RowVar & "<D" & RowVar & ",VLOOKUP(Despatches!A" & RowVar & ",OpeningOrderBookPivot!$A$3:$C$1000,3,FALSE)>0,VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$C$1000,3,FALSE)<-(Despatches!C" & RowVar & "-Despatches!D" & RowVar & ")),-VLOOKUP(A" & RowVar & ",OpeningOrderBookPivot!$A$3:$C$1000,3,FALSE),0))),0)"


        ColVar = 6

        Do While ActiveSheet.Cells(2, ColVar) <> Empty

        **Cells(RowVar, ColVar).FormulaR1C1 = "=ROUND(IF(AND(-R" & RowVar & "C3>-SUM(R" & RowVar & "C4:R" & RowVar & "C" & ColVar -1 & "),VLOOKUP(R" & RowVar & "C1,Difference!R" & RowVar & "C1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar & ",Difference!R2,0)),FALSE)>0,VLOOKUP(R" & RowVar & "C1,Difference!R" & RowVar & "C1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar & ",Difference!R2,0)),FALSE)>=-(Despatches!R" & RowVar & "C3-(SUM(R" & RowVar & "C4:R" & RowVar & "C" & ColVar - 1 & ")))),R" & RowVar & "C3-SUM(R" & RowVar & "C4:R" & RowVar & "C" & ColVar & " -1),IF(AND(-R" & RowVar & "C3>-SUM(R" & RowVar & "C4,R" & RowVar & ":C" & ColVar & " -1),VLOOKUP(R" & RowVar & ",Difference!R" & RowVar & "C1:R1000C" & ColVar & ",(Match(R2C" & ColVar & ",Difference!R2,0)),False)>0,-VLOOKUP(R" & RowVar & "C1,Difference!R" & RowVar & "C1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar & ",Difference!R2,0)),False) _
        <-(Despatches!R" & RowVar & "C3-(Sum(R" & RowVar & "C4,R" & RowVar & "C" & ColVar -1 & ")))),-VLOOKUP(R" & RowVar & "C1,Difference!R" & RowVar & "C1:R1000C" & ColVar & ",(Match(R2C" & ColVar & ",Difference!R2,0)),False),IF(VLOOKUP(Despatches!R" & RowVar & "C1,Difference!R" & RowVar & "C1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar &  ",Difference!R2,0)),False)=0,0,0))),0)"**

ColVar = ColVar + 1


            Loop


    RowVar = RowVar + 1


Loop

End Sub

1 个答案:

答案 0 :(得分:0)

您标有两个星号的代码行至少应以)" & _而不是) _结尾,下一行的开头应为"<-(而不是<-(

此外,您的所有RowVar视频都可以删除。在xlR1C1中,如果您在同一行,则RC3R" & RowVar & "C3表示相同的内容。这就像说(第5行时)C5和C $ 5而且它对你的公式没什么影响,因为你把它放在同一行上。

With ActiveSheet    'this should be better like Worksheets("Sheet1")
    Do While .Cells(2, ColVar) <> Empty
        .Cells(RowVar, ColVar).FormulaR1C1 = _
            "=ROUND(IF(AND(-RC3>-SUM(RC4:RC" & ColVar - 1 & "),VLOOKUP(RC1,Difference!RC1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar & ",Difference!R2,0)),FALSE)>0,VLOOKUP(RC1,Difference!RC1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar & ",Difference!R2,0)),FALSE)>=-(Despatches!RC3-(SUM(RC4:RC" & ColVar - 1 & "))))" & _
             ",RC3-SUM(RC4:RC" & ColVar & " -1),IF(AND(-RC3>-SUM(RC4,R" & RowVar & ":C" & ColVar & " -1),VLOOKUP(R" & RowVar & ",Difference!RC1:R1000C" & ColVar & ",(Match(R2C" & ColVar & ",Difference!R2,0)),False)>0,-VLOOKUP(RC1,Difference!RC1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar & ",Difference!R2,0)),False)" & _
             "<-(Despatches!RC3-(Sum(RC4,RC" & ColVar - 1 & ")))),-VLOOKUP(RC1,Difference!RC1:R1000C" & ColVar & ",(Match(R2C" & ColVar & ",Difference!R2,0)),False),IF(VLOOKUP(Despatches!RC1,Difference!RC1:R1000C" & ColVar & ",(MATCH(R2C" & ColVar & ",Difference!R2,0)),False)=0,0,0))),0)"

        ColVar = ColVar + 1
    Loop
End With

现在,formula-as-a-string确实编译了,但我无法在工作表上证明它的有效性。我用于复杂公式的一种方法是使用' Range.PrefixCharacter property将公式作为字符串放入工作表中。

.Cells(RowVar, ColVar).FormulaR1C1 = "'=ROUND(IF(AND(-R...

例程运行完毕后,返回工作表并尝试删除'以查看它是否是有效的公式,以及是否返回了正确的结果。