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
 
From cell G13 till first empty column (since the number of columns varies according to workbook)
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
For one workbook:
VBA Code:
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False
LastColMyFile = Workbooks(MyFile).Worksheets("Hoja1").Cells(12, Columns.Count).End(xlToLeft).Column
Workbooks.Open Filename:="C:\Users\Usuario\Documents\Datos\26.02.2021_BASE.xlsm"
Sheets("BASE").Select
LastRowBase = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range(Cells(13, 7), Cells(LastRowBase, LastColMyFile)).Formula = "=+VLOOKUP(RC1,[" & MyFile & "]Hoja1!R13C1:R500C50,+COLUMN([" & MyFile & "]Hoja1!R[1]C),FALSE)"
Workbooks(MyFile).Close SaveChanges:=False
 
Upvote 0
thanks
For one workbook:
VBA Code:
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False
LastColMyFile = Workbooks(MyFile).Worksheets("Hoja1").Cells(12, Columns.Count).End(xlToLeft).Column
Workbooks.Open Filename:="C:\Users\Usuario\Documents\Datos\26.02.2021_BASE.xlsm"
Sheets("BASE").Select
LastRowBase = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range(Cells(13, 7), Cells(LastRowBase, LastColMyFile)).Formula = "=+VLOOKUP(RC1,[" & MyFile & "]Hoja1!R13C1:R500C50,+COLUMN([" & MyFile & "]Hoja1!R[1]C),FALSE)"
Workbooks(MyFile).Close SaveChanges:=False

[/CODE
[/QUOTE]
 
Upvote 0
When +COLUMN([" & MyFile & "]Hoja1!R[1]C),FALSE)" , can you help me with this, when the file vlookingup changes the column formula has to start again, but in the code it continues, for example,

workbook1 workbook2

=+BUSCARV(....;+COLUMNA('C:....)\[C42.xls]Hoja1'!S14);FALSO) =+BUSCARV(....;+COLUMNA('C:....)\[C42.xls]Hoja1'!T14);FALSO)


workbook1 workbook2
=+BUSCARV(....;+COLUMNA('C:....)\[C42.xls]Hoja1'!S14);FALSO) =+BUSCARV(....;+COLUMNA('C:....)\[C42.xls]Hoja1'!G14);FALSO)
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,020
Members
448,543
Latest member
MartinLarkin

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