Обозначение VBA R1C1 несоответствие контуров петли - PullRequest
0 голосов
/ 03 сентября 2018

У меня есть макрос, использующий нотацию 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 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

1 Ответ

0 голосов
/ 03 сентября 2018

Эта формула не имеет смысла, как написано. Я думаю, что вы имели в виду это:

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 без номера строки означает ссылку на ту же строку, к которой относится формула.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...