+ Reply to Thread
Results 1 to 6 of 6

Trying to write a VLOOKUP with variables causes RC[1] to not be recognized as a cell refer

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2013
    Location
    California, CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Trying to write a VLOOKUP with variables causes RC[1] to not be recognized as a cell refer

    Hello,

    The code will successfully open files in alphabetical order of name and perform vlookups based on the values in column B. I am having trouble just with one piece of the code. I want to subtract the value in the column to the left from each value. Here is the code I am having a problem with:

    Range(Cells(2, pasteColumn), Cells(numD, pasteColumn)).Formula = _
    "=VLOOKUP($B2,'[" & strFile & "]" & wshSrc.Name & "'!$B:$AJ,30,FALSE)+VLOOKUP($B2,'[" & strFile & "]" & wshSrc.Name & "'!$B:$AJ,32,FALSE)-IF(ISNUMBER(RC[-1],rc[-1],0)"
    It works without the last part:

    "=VLOOKUP($B2,'[" & strFile & "]" & wshSrc.Name & "'!$B:$AJ,30,FALSE)+VLOOKUP($B2,'[" & strFile & "]" & wshSrc.Name & "'!$B:$AJ,32,FALSE)"
    But I need to subtract the values.

    Thanks in advance for help. Here is the full code:

    Sub ProcessFiles()
        Dim strPath As String
        Dim strFile As String
        Dim wbkSrc As Workbook
        Dim wshSrc As Worksheet
        Dim wbkTrg As Excel.Workbook
        Dim numD As Long
        Dim pasteColumn As Long
        Set wbkTrg = ActiveWorkbook
        numDealers = Cells(Rows.Count, "A").End(xlUp).Row
        pasteColumn = 3
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                MsgBox "You didn't select a folder", vbExclamation
                Exit Sub
            End If
        End With
        Application.ScreenUpdating = False
        If Right(strPath, 1) <> "\" Then
            strPath = strPath & "\"
        End If
        Debug.Print (strPath)
        strFile = Dir(strPath & "*.xls*")
        Do While strFile <> ""
            Set wbkSrc = Workbooks.Open(strPath & strFile)
            Set wshSrc = wbkSrc.Worksheets(1)
            Debug.Print (strFile)
            Debug.Print (wshSrc.Name)
            wbkTrg.Activate
            Debug.Print (pasteColumn)
            Range(Cells(2, pasteColumn), Cells(numD, pasteColumn)).Formula = _
                "=VLOOKUP($B2,'[" & strFile & "]" & wshSrc.Name & "'!$B:$AJ,30,FALSE)+VLOOKUP($B2,'[" & strFile & "]" & wshSrc.Name & "'!$B:$AJ,32,FALSE)-IF(ISNUMBER(RC[-1],rc[-1],0)"
            Range(Cells(2, pasteColumn), Cells(numD, pasteColumn)).Copy
            Range(Cells(2, pasteColumn), Cells(numD, pasteColumn)).PasteSpecial xlPasteValues
            pasteColumn = pasteColumn + 1
            wbkSrc.Close SaveChanges:=False
            strFile = Dir
        Loop
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    Last edited by jeffreybrown; 06-19-2013 at 08:57 PM. Reason: Please use code tags...Thanks.

  2. #2
    Registered User
    Join Date
    06-19-2013
    Location
    California, CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Trying to write a VLOOKUP with variables causes RC[1] to not be recognized as a cell r

    Bumping..........

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Trying to write a VLOOKUP with variables causes RC[1] to not be recognized as a cell r

    Try this...
    Range(Cells(2, pasteColumn), Cells(numD, pasteColumn)).FormulaR1C1 = _
        "=VLOOKUP(RC2,'[" & strFile & "]" & wshSrc.Name & "'!C2:C36,30,FALSE)" & _
        "+VLOOKUP(RC2,'[" & strFile & "]" & wshSrc.Name & "'!C2:C36,32,FALSE)" & _
        "-IF(ISNUMBER(RC[-1]),RC[-1],0)"
    Last edited by AlphaFrog; 06-19-2013 at 10:16 PM.

  4. #4
    Registered User
    Join Date
    06-19-2013
    Location
    California, CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Trying to write a VLOOKUP with variables causes RC[1] to not be recognized as a cell r

    Hey, thanks. That does work, however I just realized that I am trying to subtract every value that is to the left of the cell. So that if that if there were two cells of information to the left of the current cell, both would be subtracted:

    2 3 =x-(2+3) =y-(2+3+x)

    Do you or anyone else have any clue how to write a formula that would do this>?

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Trying to write a VLOOKUP with variables causes RC[1] to not be recognized as a cell r

    Quote Originally Posted by RichFroning View Post
    Hey, thanks. That does work, however I just realized that I am trying to subtract every value that is to the left of the cell. So that if that if there were two cells of information to the left of the current cell, both would be subtracted:

    2 3 =x-(2+3) =y-(2+3+x)

    Do you or anyone else have any clue how to write a formula that would do this>?
    I don't follow.

  6. #6
    Registered User
    Join Date
    06-19-2013
    Location
    California, CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Trying to write a VLOOKUP with variables causes RC[1] to not be recognized as a cell r

    Oops. Those were meant to look like cells. I want to find the change each day in a number that is a total. So if on the first 3 days of the month it is
    3,5,10, I want the cells to read 3,2,5. The issue is that just subtracting the previous cell the way it is now would give you 3, 2, 8.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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