CatLadee
New Member
- Joined
- Sep 7, 2018
- Messages
- 29
Hi all,
Appreciate your help in advance. I have this code and it's purpose is to loop through files in a folder to open them up. From there, a master worksheet will vlookup values from two columns to pull in the cells values where they match the HASH ID (in both files). How do I setup the vlookup formula to have a variable that represents each worksheet? Also open to suggestions on alternative ways to accomplish the same result - thanks! Lindsay
Appreciate your help in advance. I have this code and it's purpose is to loop through files in a folder to open them up. From there, a master worksheet will vlookup values from two columns to pull in the cells values where they match the HASH ID (in both files). How do I setup the vlookup formula to have a variable that represents each worksheet? Also open to suggestions on alternative ways to accomplish the same result - thanks! Lindsay
Code:
Sub Consolidate()
'Excel VBA code to loop through files in a folder with Excel VBA
Dim MyFolder As String, MyFile As String
'Opens a file dialog box for user to select a folder
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
MyFolder = .SelectedItems(1)
Err.Clear
On Error GoTo 0
End With
If MyFolder = "" Then End
'stops screen updating, calculations, events, and statsu bar updates to help code run faster
'you'll be opening and closing many files so this will prevent your screen from displaying that
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'This section will loop through and open each file in the folder you selected
'and then close that file before opening the next file
MyFile = Dir(MyFolder & "\", vbReadOnly)
Do While MyFile <> ""
DoEvents
On Error GoTo 0
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False
' ''''''''''''ENTER YOUR CODE HERE TO DO SOMETHING'''''''''
Workbooks.Open Filename:="H:\HSI\LEAP\LEAP Project Workbook.xlsm"
Sheets("Master").Select
Range("O2").Select
ActiveCell.FormulaR1C1 = ""
Range("O2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP([@[Hash ID]],'Sheet1'!C1:C16,15,FALSE)"
Range("P2").Select
ActiveCell.FormulaR1C1 = ""
Range("P2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP([@[Hash ID]],'Sheet1'!C1:C16,16,FALSE)"
Workbooks(MyFile).Close SaveChanges:=False
MyFile = Dir
Loop
'
' 'turns settings back on that you turned off before looping folders
'
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlCalculationManual
End Sub