Results 1 to 6 of 6

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

Threaded 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.

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