I am trying to get information out of a file name and assign it to a variable using VBA, formula works. Can't get variable assignment to work

Dim InvDate As String
    Dim Ret As Variant

Ret is the result of an object that gets a file name.

Returned filename in Ret is: C:\Users\John Q. Public\Documents\CompanyDir\Name\LowVoltageProducts\Client\ComplianceMonitor\Cmdata\Inventory15-08-03.xlsx

The following formula gets the file date from the file name when using it in Excel. I want to save the date in a variable and copy name to another cell in a different worksheet.

Code crashes here:
InvDate.Formula = "= Right(Left(Ret, Find(""."", Ret, 20) - 1), 8)"

Error msg is "Invalid Qualifier" and InvDate is highlighted,
EdLBAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Define invDate as "Range".
regmigrantCommented:
You've defined invdate as a string  so the .formula doesn't mean anything to VBA at that point

try InvDate  = Right(Left(Ret, Find(""."", Ret, 20) - 1), 8) - if you are trying to assign invdate as a string or define invdate as a range and give it a cell address if you are trying to put the formula into a sheet.
aikimarkCommented:
You can also these expressions:
String result:
mid(ret,instrrev(ret,".")-8,8)

Open in new window

Datetime result:
CDate(mid(ret,instrrev(ret,".")-8,8))

Open in new window


If the file name is coming from a filesystemobject, there are other ways of parsing the filename.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

EdLBAuthor Commented:
When I try  InvDate = Right(Left(Ret, Find(""."", Ret, 20) - 1), 8)

I get a syntax error and the entire line is highlighted.
EdLBAuthor Commented:
Aikimark, there are two periods in the Ret filename which is why I used the Find and started at 20.
regmigrantCommented:
Sorry - I just copied the code and didn't think about the conversion to VBA. find has to be replaced with Instr which has its arguments the opposite way round - obviously the filename should be changed to your "ret" variable but I needed to prove it works this time :)


invdate = Right(Left("C:\Users\John Q. Public\Documents\CompanyDir\Name\LowVoltageProducts\Client\ComplianceMonitor\Cmdata\Inventory15-08-03.xlsx" _
  , InStr(20, "C:\Users\John Q. Public\Documents\CompanyDir\Name\LowVoltageProducts\Client\ComplianceMonitor\Cmdata\Inventory15-08-03.xlsx", ".") - 1), 8)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EdLBAuthor Commented:
That did it. Thanks! Is there some legend somewhere that lists what EXCEL functions work in VBA or what the VBA equivalents of EXCEL functions are?
aikimarkCommented:
That is why I used InstrRev() -- to search starting from the end of the string
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.