Assigning a file and path to a variable within VBA to be inserted as a formula

Woftae

New Member
Joined
Feb 10, 2018
Messages
16
To minimise the amount of changes I need to make when this bit of code is duplicated for other filenames, ALPHA-L4L.xlsm changing to BRAVO-L4L.xlsm and so on, I assigned the .xlsm file and its path to a variable so that I only need to make the change once (see black and bolded). Which is utilised successfully in Range("W1").Value = "L4L" & vbLf & Format(FileDateTime(FilePath), "dd mmm")
But I also need to utilise this variable within the code which will be inserted as a formula in to a cell (see red and bolded) but I am unable to get it to work.
I have tried concatenating the [ ] into the appropriate locations but can not get it to accept it and work.
What is the correct method/procedure/syntax to achieve this??
Thanking the many in advance for their assistance.

Dim FilePath As String
FilePath = "Z:\Price Updates\Scrap\ALPHA-L4L.xlsm"

Range("W:X").EntireColumn.Hidden = False

If Columns("W").Hidden = False Then
Range("W1").Value = "L4L" & vbLf & Format(FileDateTime(FilePath), "dd mmm")
Range("W3").Formula = "=IFERROR(NUMBERVALUE(VLOOKUP(A2,'Z:\Price Updates\Scrap\[ALPHA-L4L.xlsm]SCRAP Latest'!$A:$F,4,FALSE)),""-"")"
Range("X3").Formula = "=IF(OR(ISERROR(NUMBERVALUE(VLOOKUP(A2,'Z:\Price Updates\Scrap\[ALPHA-L4L.xlsm]SCRAP Previous'!$A:$F,4,FALSE))),ISERROR(NUMBERVALUE(VLOOKUP(A2,'Z:\Price Updates\Scrap\[ALPHA-L4L.xlsm]SCRAP Previous'!$A:$F,4,FALSE)))),""""," _
& Chr(10) & "IF(VLOOKUP(A2,'Z:\Price Updates\Scrap\[ALPHA-L4L.xlsm]SCRAP Latest'!$A:$F,4,FALSE)<VLOOKUP(A2,'Z:\Price Updates\Scrap\[ALPHA-L4L.xlsm]SCRAP Previous'!$A:$F,4,FALSE),""ò""," _
& Chr(10) & "IF(VLOOKUP(A2,'Z:\Price Updates\Scrap\[ALPHA-L4L.xlsm]SCRAP Latest'!$A:$F,4,FALSE)>VLOOKUP(A2,'Z:\Price Updates\Scrap\[ALPHA-L4L.xlsm]SCRAP Previous'!$A:$F,4,FALSE),""ñ"",""ó"")))"
Else
Range("W1").Value = "L4L" & vbLf & "IGNORE"
Range("W2").Value = "-"
Range("X2").Value = ""
End If
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I worked out a syntax that worked for me
=IFERROR(NUMBERVALUE(VLOOKUP(A2,'" & VariableForFilePath & "[" & VariableForFile & ".xlsm]" & VariableForSheet & "'!$A:$D,4,FALSE)),""-"")"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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