VBA filename into cell error

Dani_LobP

Board Regular
Joined
Aug 16, 2019
Messages
134
Office Version
  1. 365
Platform
  1. Windows
Hi,

I just created some macro that does a simply task.

Asks to open a CSV file, and then in that file adds 2 formulas.

Range("I10").Formula = "=MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1, SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-1)"
*I took this one from internet googling how to get filename without full path.

Range("H10").Formula = "=LEFT(I10,(FIND(""_"",I10,1)-1))"

The idea is to add the filename into a cell, and from that cell take just the start which is what I actually need.

My problem here, is that sometimes formula will work, and in other situations I get #VALUE! error...
When I check the error, I see the below in the formula 1 cell:

=MID(@CELL("filename"),SEARCH("[",@CELL("filename"))+1, SEARCH("]",@CELL("filename"))-SEARCH("[",@CELL("filename"))-1)

I try removing the @ symbols, but would still give error.
if i try =Cells(Filename) i will notice that there is no [] .. so probably error comes from there. But I have no clue in why sometimes filename will bring [] and why other times it won't...

Hope someone can help me out here or provide a workaround for the same purpose.

Thanks in advance.


 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
for now fixed with workaround using activeworkbook.name to obtain in the cell the file name.... i guess it will work and basically do the same.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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