Excel VBA Vlookup using 2 workbooks

tanyaleblanc

Board Regular
Joined
Mar 16, 2019
Messages
145
Hi, I'm new to VBA and don't quite really understand much, I apologize, I'm trying to do a Vlookup from another workbook.

All I want to do is in cell b9 in my current workbook, search for values in another workbook (Finance Extract.xls) from cells d9 to last cell and return a value
Here is the code I used but it's returning a zero (0) in each row in column b

Range("b9:b" & lRow).Formula = "=iferror(VLOOKUP(d9,Finance Extract.xls!d:d,4,FALSE),0)"

What am I doing wrong?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Re: Translate Vlookup to vba code

You are missing an apostrophe ' between Sheet1 & ! it should be Sheet1'!
 
Upvote 0
Re: Translate Vlookup to vba code

I'm getting closer, it worked, but it didn't copy the whole account # only the reference number. Column D on the sheet that Im looking up, has a formula in it - =VALUE(VALUE(E9)&VALUE(G9)), this formula gives me the value of E9 which is the account# and the value of G9 which is the reference #. eg - account# 1100726 Reference#15292, together in cell D9 is 110072615292, but what populated in cell B9 is 15292, just the ref#, not both. Why would that be?
 
Upvote 0
Re: Translate Vlookup to vba code

What is in D9 on the sheet containing the formula? and what do you want to return if that value is found?
At the moment you are searching Col D in the other workbook for a value & if found returning the value from col G (the 4the column in the lookup range)
 
Upvote 0
Re: Translate Vlookup to vba code

Hello, I didn't know how to post a new question, up top it says click the +Post New Thread button, but I don't see one. In this example, I'm trying to copy a column of data (J) from one workbook to the last row in column E of another workbook. I used the below and the first copy and paste from the Finance extract worked perfectly, but the second copy and paste from the rental extract, pasted the data from col J into col E but on the last row in column E, but it should have pasted in row E52, I think the reason why is that there is data in col C and D that goes beyond row 52 and it thinks it should paste it in col E on the last row which is row E7150. I just want to paste it in the last cell in col E, which should be E52.

Sub Admin_Fee()
Dim fd As Office.FileDialog
Dim lRow As Long
Dim wb1 As Workbook, wb2 As Workbook
Dim sh As Worksheet
Dim lrownew As Long
If AutoFilterMode = True And FilterMode = True Then ActiveSheet.ShowAllData


Set fd = Application.FileDialog(msoFileDialogFilePicker)


With fd
.AllowMultiSelect = False
.Title = "Please select the file."
'
' This stage, all of the questionable contracts should have been sorted out and you should be able to compile a list to charge maxium off of.
'


'
' Clear out the current filters, and add our own.
.Filters.Clear
'.Filters.Add "Excel 2003", "*.xls"
'.Filters.Add "All Files", "*.*"

' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtfilename = .SelectedItems(1) 'replace txtFileName with your textbox
'MsgBox txtfilename
Workbooks.Open txtfilename
End If
End With


Windows("Finance Extract.xls").Activate
lRow = Cells(Rows.Count, "F").End(xlUp).Row
Range("F9:F" & lRow).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Admin Fee.xls").Activate
lrownew = Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("E6").PasteSpecial Paste:=xlPasteValues
With fd
.AllowMultiSelect = False
.Title = "Please select the file."
'
' This stage, all of the questionable contracts should have been sorted out and you should be able to compile a list to charge maxium off of.
'


'
' Clear out the current filters, and add our own.
.Filters.Clear
'.Filters.Add "Excel 2003", "*.xls"
'.Filters.Add "All Files", "*.*"

' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtfilename = .SelectedItems(1) 'replace txtFileName with your textbox
'MsgBox txtfilename
Workbooks.Open txtfilename
End If
End With
Windows("Rental Extract.xls").Activate
lRow = Cells(Rows.Count, "F").End(xlUp).Row
Range("J11:J" & lRow).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Admin Fee.xls").Activate
lrownew = Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("E" & lrownew).PasteSpecial Paste:=xlPasteValues
 
Upvote 0
Re: Translate Vlookup to vba code

As this is a new question you need to start a new thread.
Also when posting code please use code tags, the # icon in the reply window.
Thanks.
 
Upvote 0
Re: VBA copy data range from worksheets into new workbook

Thank you, I got it to work, it was looking in C on and the data in that col went down to 7149 and I changed it to E and it pasted in the last cell in E, yahoo!!

Thank you again,
 
Upvote 0
Re: VBA copy data range from worksheets into new workbook

Now I'm trying to copy from a cell within the same sheet to a column and copy all the way down to the last cell.

I'm trying to copy the word Rental in cell L1 to the last blank cell in G all the way down in G by using the rows in E. The first copy and paste worked, I copied cell K1 to G 6 and down to last cell, now I want to copy L1 in the next blank cell in G down to the bottom of G using the account# found in col E.



Code:
Sub Admin_Fee()
Dim fd As Office.FileDialog
Dim lRow As Long
Dim wb1 As Workbook, wb2 As Workbook
Dim sh As Worksheet
Dim lrownew As Long
If AutoFilterMode = True And FilterMode = True Then ActiveSheet.ShowAllData


Set fd = Application.FileDialog(msoFileDialogFilePicker)


With fd
.AllowMultiSelect = False
.Title = "Please select the file."
'
' This stage, all of the questionable contracts should have been sorted out and you should be able to compile a list to charge maxium off of.
'


'
' Clear out the current filters, and add our own.
.Filters.Clear
'.Filters.Add "Excel 2003", "*.xls"
'.Filters.Add "All Files", "*.*"
    
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtfilename = .SelectedItems(1) 'replace txtFileName with your textbox
'MsgBox txtfilename
Workbooks.Open txtfilename
End If
End With


Windows("Finance Extract.xls").Activate
lRow = Cells(Rows.Count, "F").End(xlUp).Row
Range("F9:F" & lRow).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Admin Fee.xls").Activate
lrownew = Cells(Rows.Count, "C").End(xlUp).Row + 1
Range("E6").PasteSpecial Paste:=xlPasteValues
With fd
.AllowMultiSelect = False
.Title = "Please select the file."
Windows("Admin Fee.xls").Activate
lastrow = Cells(Rows.Count, "E").End(xlUp).Row
Range("K1").Select
Selection.Copy
Range("G6").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("G6:G" & lastrow)
Range("G6:G" & lastrow).Select
'
' This stage, all of the questionable contracts should have been sorted out and you should be able to compile a list to charge maxium off of.
'


'
' Clear out the current filters, and add our own.
.Filters.Clear
'.Filters.Add "Excel 2003", "*.xls"
'.Filters.Add "All Files", "*.*"
    
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
txtfilename = .SelectedItems(1) 'replace txtFileName with your textbox
'MsgBox txtfilename
Workbooks.Open txtfilename
End If
End With
Windows("Rental Extract.xls").Activate
lRow = Cells(Rows.Count, "F").End(xlUp).Row
Range("J11:J" & lRow).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Admin Fee.xls").Activate
lrownew = Cells(Rows.Count, "E").End(xlUp).Row + 1
Range("E" & lrownew).PasteSpecial Paste:=xlPasteValues
[B] Windows("Admin Fee.xls").Activate[/B]
[B] lastrow = Cells(Rows.Count, "G").End(xlUp).Row[/B]
[B] Range("L1").Select[/B]
[B] Selection.Copy[/B]
[B] Range("G" & lRow).Select[/B]
[B] ActiveSheet.Paste[/B]
[B] Application.CutCopyMode = False[/B]
[B] Selection.AutoFill Destination:=Range("G" & lastrow)[/B]
[B] Range("G" & lastrow).Select[/B]
 
Last edited by a moderator:
Upvote 0
Re: VBA copy data range from worksheets into new workbook

Please do not keep asking new questions on this thread, but start a new thread.
Also when posting replies please ensure that you post them to the correct thread. Your reply in post#17 was posted to a completely different thread.

And as mention before,please use code tags when posting code.
Thanks
 
Upvote 0
Re: VBA copy data range from worksheets into new workbook

ok, I don't know how to post a new thread, the +Post new thread button up top, I don't see it anywhere. I'm not sure how my post 17 was posted to a different thread, I thought I posted everything in this thread. I didn't know about code tags, but I'll do that going forward.

Can you tell me how I post a new thread.

Sorry for any inconvenience.
 
Upvote 0

Forum statistics

Threads
1,215,901
Messages
6,127,644
Members
449,394
Latest member
fionalofthouse

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