VBA R1C1表示法循环参考不匹配

问题描述 投票:0回答:1

我有一个宏使用R1C1表示法循环数据并产生估计数据和实际数据之间的差异。我希望能够产生百分比差异,但我无法获得参考。我的代码是:

     Worksheets("IPT Bill Lab").Cells(intLoopCount, 17).FormulaR1C1 = "=R" & intLoopCount & "(C9/R" & intLoopCount & "C6,)-1"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 17).NumberFormat = "%0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 17).Select

从第5行开始。

我的excel表给我的第一个数据点是=$5:$5($I:$I/$F$5,)-1但我确定我想要=$5:$5($I:$5/$F$5,)-1

这些列都显示#REF!。我该如何解决?

这是Sub的问题。

   Sub AddBLabEstimationFields(DataPresent As Boolean)

    ' Add estimation fields to BL Sheet

   Dim intStartRow As Integer
   Dim intStartCol As Integer
   Dim intEndRow As Integer
   Dim intEndCol As Integer
   Dim intLoopCount As Integer
   Dim strSelectionName As String
   Dim strFormulaString As String
   '

Worksheets("IPT Bill Lab").Select
Worksheets("IPT Bill Lab").Range("A1").Select

Cells.Find(What:="Grand Total", After:=ActiveCell, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

'row above is last row we want
intEndRow = ActiveCell.Row - 1

'clear old data and formulae
intStartRow = 5
intEndCol = 20
intStartCol = 4
Worksheets("IPT Bill Lab").Range(Cells(intStartRow, intStartCol), Cells(100, intEndCol)).Select
Selection.Activate
Selection.Delete

'ActiveSheet.Shapes.Range(Array("NoBillLabour")).Select
If DataPresent Then

    If intEndRow > 4 Then 'there is data
        ActiveSheet.Shapes.Range(Array("NoDataValues")).Visible = msoFalse

        'Set names on totals row....
        strFormulaString = "='IPT Bill Lab'!R" & (intEndRow + 1) & "C2"
        ActiveWorkbook.Names.Add Name:="LabBillHours", RefersToR1C1:=strFormulaString

        strFormulaString = "='IPT Bill Lab'!R" & (intEndRow + 1) & "C3"
        ActiveWorkbook.Names.Add Name:="LabBillCost", RefersToR1C1:=strFormulaString

        'loop thru adding new formulae
        For intLoopCount = intStartRow To intEndRow
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 4).FormulaR1C1 = "=R" & intLoopCount & "C3/R" & intLoopCount & "C2"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 4).NumberFormat = "0.0000"

            '"=R" & intLoopCount & "C3/BL_WeeksInMonth*(BL_WeeksInMonth-BL_WeeksOfData)"
            'estimated hours
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 6).FormulaR1C1 = "=R" & intLoopCount & "C2/BLab_WeeksOfData*(BLab_WeeksInMonth-BLab_WeeksOfData)"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 6).NumberFormat = "0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 6).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 13434879
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

            'estimated costs
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 7).FormulaR1C1 = "=R" & intLoopCount & "C6*R" & intLoopCount & "C4"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 7).NumberFormat = "$#,##0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 7).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 13434879
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

            'IPT Hours
            'Worksheets("IPT Bill Lab").Cells(intLoopCount, 9).FormulaR1C1 = "=R" & intLoopCount & "C6+R" & intLoopCount & "C2"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 9).FormulaR1C1 = "=R" & intLoopCount & "C2"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 9).NumberFormat = "0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 9).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 5296274
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

            'IPT costs
            'Worksheets("IPT Bill Lab").Cells(intLoopCount, 10).FormulaR1C1 = "=R" & intLoopCount & "C7+R" & intLoopCount & "C3"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 10).FormulaR1C1 = "=R" & intLoopCount & "C3"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 10).NumberFormat = "$#,##0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 10).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 5296274
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
                            '=LEN(A5)-FIND("@",SUBSTITUTE(A5," ","@",LEN(A5)-LEN(SUBSTITUTE(A5," ",""))))+1
            strFormulaString = "=LEN(R" & intLoopCount & "C1)-FIND(""@"",SUBSTITUTE(R" & intLoopCount & "C1,"" "",""@"",LEN(R" & intLoopCount & "C1)-LEN(SUBSTITUTE(R" & intLoopCount & "C1,"" "",""""))))+1"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 12).FormulaR1C1 = strFormulaString

            '=RIGHT(A5,L5-1)
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 13).FormulaR1C1 = "=RIGHT(R" & intLoopCount & "C1,R" & intLoopCount & "C12-1)"

            '=LEFT(A5,LEN(A5)-L5)
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 14).FormulaR1C1 = "=LEFT(R" & intLoopCount & "C1,LEN(R" & intLoopCount & "C1)-R" & intLoopCount & "C12)"

            '=$I5-$F5


            Worksheets("IPT Bill Lab").Cells(intLoopCount, 16).FormulaR1C1 = "=R" & intLoopCount & "C9-R" & intLoopCount & "C6"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 16).NumberFormat = "0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 16).Select

            '=($I5-$F5)-1 percentage hours
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 17).FormulaR1C1 = "=R" & intLoopCount & "(C9/R" & intLoopCount & "C6,)-1"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 17).NumberFormat = "%0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 17).Select

            '=$J5-$G5

            Worksheets("IPT Bill Lab").Cells(intLoopCount, 18).FormulaR1C1 = "=R" & intLoopCount & "C10-R" & intLoopCount & "C7"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 18).NumberFormat = "$#,##0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 18).Select

            '=($J5-$G5)-1 percentage change cost
                                                                            '"=R" & intLoopCount & "C3/R" & intLoopCount & "C2"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 19).FormulaR1C1 = "=R" & intLoopCount & "(C10/R" & intLoopCount & "C7)-1"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 19).NumberFormat = "%0.00"
            Worksheets("IPT Bill Lab").Cells(intLoopCount, 19).Select

        Next intLoopCount
vba excel-vba loops excel-formula excel-r1c1-notation
1个回答
0
投票

这个公式没有意义。我想你的意思是:

With Worksheets("IPT Bill Lab").Cells(intLoopCount, 17)
    .FormulaR1C1 = "=(RC9/RC6)-1"
    .NumberFormat = "0.00%"
    With .Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 5296274
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
End With

请注意,使用不带行号的R表示对公式所在的同一行的引用。

© www.soinside.com 2019 - 2024. All rights reserved.