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
Bookmarks