Excel VBA for pasting formula with a Path and a Variable

pmacedge

New Member
Joined
Jul 12, 2023
Messages
2
Office Version
  1. 365
Hi all, I'm new in the forum as well as with VBA
I'm trying to program a macro so I can paste a specific formula into a cell including a path and a variable within it. Here's the code I'm trying:


Dim Path_VL As String
Dim Date_VL As String

Date_VL = Range("A3").Text

Path_VL = "=VLOOKUP(""09:35"";'C:\Users\pffma\Desktop\Folder1\[ " & Date_VL & ".xlsx]Sheet1'!$B:$F;2;FALSE)" 'This is the formula I would like to paste when running the macro...

Range("B4").Select
ActiveCell.FormulaR1C1 = Path_VL 'Here is the cell I want to paste the formula but it's debugging here
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Maybe this:
VBA Code:
    Dim Path_VL As String
    Dim Date_VL As String
    Dim FilePath As String
    Dim SearchTerm As String
   
    SearchTerm = "09:35"
    Date_VL = Range("A3").Text
    FilePath = "C:\Users\pffma\Desktop\Folder1\" & Date_VL & ".xlsx"
   
   
    Path_VL = "=VLOOKUP(" & SearchTerm & ",'[" & FilePath & "]Sheet1'!$B:$F,2,FALSE)" 'This is the formula I would like to paste when running the macro...
    Range("B4").Formula = Path_VL
 
Upvote 0
Maybe this:
VBA Code:
    Dim Path_VL As String
    Dim Date_VL As String
    Dim FilePath As String
    Dim SearchTerm As String
  
    SearchTerm = "09:35"
    Date_VL = Range("A3").Text
    FilePath = "C:\Users\pffma\Desktop\Folder1\" & Date_VL & ".xlsx"
  
  
    Path_VL = "=VLOOKUP(" & SearchTerm & ",'[" & FilePath & "]Sheet1'!$B:$F,2,FALSE)" 'This is the formula I would like to paste when running the macro...
    Range("B4").Formula = Path_VL
Working beatifully :))) Thank you so much you really helped
 
Upvote 0

Forum statistics

Threads
1,215,366
Messages
6,124,516
Members
449,168
Latest member
CheerfulWalker

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