Insert VLOOKUP into cells as a formula

Smurphy820

New Member
Joined
May 25, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello I need some help with inserting VLOOKUP into cells as a formula. The first part of code finds the document that I need to pull data from. My plan was to essentially hard code the formula into all of the cells I need using VBA. Both tabs have the same name, so I thought I could just input the document's name as a variable and put the tab's name after it to complete the middle section of the VLOOKUP formula.

Sub ASM__tabs()


Dim sFound As String
Dim folderpath As String
Dim Sheettitle As String


Name = ActiveWorkbook.Name
namesplit = Split(Name, "-")
Namesplitfinal = namesplit(0)

folderpath = "C:\Users\xxx\OneDrive - xxx\Documents\xxx\Macros\" & Namesplitfinal & "\"

sFound = Dir(folderpath & "Monthly Membership by County*.xlsx")
If sFound <> "" Then
Workbooks.Open Filename:=folderpath & "\" & sFound
Name = ActiveWorkbook.Name
End If

ThisWorkbook.Activate
Worksheets(1).Select
Sheettitle = ActiveSheet.Name

Range("E23:E32").Formula = "=VLOOKUP($B23," & Name & "" & Sheettitle & "!$A$3:$E$261,3,FALSE)"

This is the table I am putting the formula into.
1653692768696.png


And here is the tab I'm pulling data from.

1653693195265.png




The problem is the code won't even run, I keep getting this error and it highlights the section of the code with the Vlookup. So I think I am way off and would appreciate some help! thanks!!

1653692947544.png






End Sub
 

Attachments

  • 1653692915996.png
    1653692915996.png
    43.1 KB · Views: 4

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this way:
-open manually one of the target workbooks ("Monthly Membership by County*.xlsx")
-now insert on the starting workbook the VLOOKUP formula
-examine the ntax of the formula
This will be like =VLOOKUP($B23,'[NameOfTheFile.xlsx]4-22'!$A$3:$E$261,3,FALSE)

Well, your instruction lacks inserting both the square brackets [ ] and both the single-quotation-marks '

Try
VBA Code:
Range("E23:E32").Formula = "=VLOOKUP($B23, '[" & Name & "]" & Sheettitle & "'!$A$3:$E$261,3,FALSE)"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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