Vlookup in vba

CamVilla

New Member
Joined
Feb 8, 2021
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
Hi, I am new with the VBA, can someone help me, I have made this macro, but the loop across files in a directory runs only in cell G13, and I would like that, once it detects that the value is in a file, pastes it, and continues with the next cell. In other words, my macro result is the value of the last file that appears.

Sub Vlookup()
Dim MyFolder As String, MyFile As String
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

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

MyFile = Dir(MyFolder & "\", vbReadOnly)
Do While MyFile <> ""
DoEvents
On Error GoTo 0
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False

Workbooks.Open Filename:="C....xlsm"
Sheets("BASE").Select
Range("G13").Select
'Range("G13:M21").Formula = "=VLOOKUP(RC1,[" & MyFile & "]Hoja1!R13C1:R500C50,+COLUMN([" & MyFile & "]Hoja1!R[1]C),FALSE)"
ActiveCell.FormulaR1C1 = _
"=+VLOOKUP(RC1,[" & MyFile & "]Hoja1!R13C1:R500C50,+COLUMN([" & MyFile & "]Hoja1!R[1]C),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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
VBA Code:
iRow=13
Do While MyFile <> ""
.....
Cells(iRow,7).FormulaR1C1 = "=+VLOOKUP(RC1,[" & MyFile & "]Hoja1!R13C1:R500C50,+COLUMN([" & MyFile & "]Hoja1!R[1]C),FALSE)"
iRow=iRow+1
 
Upvote 0
Do you get an error or what happend?
Can you post your macro.
 
Upvote 0
Error no, but it only works in 3 cells.

//////////
Sub Vlookup()

Dim MyFolder As String, MyFile As String

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

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual


MyFile = Dir(MyFolder & "\", vbReadOnly)
iRow = 13
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:="C:\Users\Usuario\Documents\Datos\26.02.2021_BASE.xlsm"
Sheets("BASE").Select
Cells(iRow, 7).FormulaR1C1 = "=+VLOOKUP(RC1,[" & MyFile & "]Hoja1!R13C1:R500C50,+COLUMN([" & MyFile & "]Hoja1!R[1]C),FALSE)"
iRow = iRow + 1


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
//////////
 

Attachments

  • Imagen1.png
    Imagen1.png
    85.3 KB · Views: 15
Upvote 0
How many files are there in the folder?
The last Application.Calculation = xlCalculationManual --> why not automatic?
COLUMN([" & MyFile & "]Hoja1!R[1]C) --> this is a relative address, is that correct?
 
Upvote 0
there are many files, that why I dont want to make reference to each file, better automatic.
The last Application.Calculation = xlCalculationManual --> why not automatic? Yes better automatic
COLUMN([" & MyFile & "]Hoja1!R[1]C) --> this is a relative address, is that correct? What do you mean by relative? I think it is correct. Because each workbook in the file has more than one column (image uploaded). But there is no pattern of the numbers of columns of each workbook, identifing the last empty cell between loops?
 

Attachments

  • Imagen1.png
    Imagen1.png
    84.2 KB · Views: 12
Upvote 0
Yes, "myfile" are the workbooks in the folder. The data does not have always the same number of columns, but, the value to lookup is always the column A. Iam trying to identfy the last empty column and continue from there the loop across the workbooks in the file.
 

Attachments

  • Imagen3.png
    Imagen3.png
    246.1 KB · Views: 14
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,737
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top