+ Reply to Thread
Results 1 to 2 of 2

AutoFilter FormulaR1C1 R[-1] Error

  1. #1
    Registered User
    Join Date
    01-03-2005
    Posts
    35

    AutoFilter FormulaR1C1 R[-1] Error

    Hi,

    I've got a spreadsheet where I'm trying to apply populate a formula into certain rows. In order to try and avoid lengthy loops, I'm trying to filter based on certain criteria, and just apply the formula to those rows.

    So, I kept getting #REF! errors in the cells where I was populating the formula, so I've stripped it down to the very basics, and essentially, it's having trouble with the negative R[-1] in the formula. So I've got the following code:

    PHP Code: 
    Sub PopulateFormulae()

    Dim WS As Worksheet

    Set WS 
    Sheets("Sheet 1")

    'Go through each of the years where the formula should be appplied
    For i = 2010 To 2014 Step 1

    WS.Range("A2").AutoFilter Field:=7, Criteria1:="=" & i

        '
    Add the formula
        With WS

            With 
    .AutoFilter.Range

            
    .Columns(14).Cells.FormulaR1C1 "=R[-1]C"

            
    End With

        End With

    Next i

    End Sub 
    If I replace the:
    PHP Code: 
    "=R[-1]C" 
    with

    PHP Code: 
    "=R[1]C" 
    it all works fine, but the -1 keeps bringing up #REF! errors.

    I thought it might be because the row above is not in the filtered range, but then neither is the row after (ie the R[1] row)

    I have noticed that it is also adding the formula into row 1 (ie my header row) and wondered if this might be related. However, I don't understand why that would bring up an error in every row.

    Does anyone have any ideas where I'm going wrong?

    Thanks!
    Last edited by grey; 05-18-2010 at 10:30 AM.

  2. #2
    Registered User
    Join Date
    01-03-2005
    Posts
    35

    Re: AutoFilter FormulaR1C1 R[-1] Error

    OK, it seems that just posting this problem helped me figure out the answer in about 2 minutes flat. For anyone else that has a similar problem. The Offset on the Autofilter Range seems to have worked:

    PHP Code: 
    Sub PopulateFormulae()


    Dim WS As Worksheet

    Set WS 
    Sheets("Sheet 1")

    'Go through each of the years where the formula should be appplied 
    For i = 2010 To 2014 Step 1

    WS.Range("A2:A2").AutoFilter Field:=7, Criteria1:="=" & i

    '
    Add the formula 
    With WS
    With 
    .AutoFilter.Range

    .Offset(1).Resize(.Rows.Count 1).Columns(14).Cells.FormulaR1C1 "=R[-1]C"

    End With

    End With

    Next i

    End Sub 

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1