Solved

VBA  one liner

Posted on 2014-09-24
4
218 Views
Last Modified: 2014-09-25
Hello,
There will always be one file in a folder. The only thing is that the file will have a date stamp in it from day to day. What is one liner code in vba that  lets me set an object to that one and noly file?
Like
Set wb = folder.files(*.xls)
0
Comment
Question by:Rayne
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40343258
Here you go:
Set wb = Application.Workbooks.Open(strPath + Dir(strPath))

You can either assign strPath to a string that represents the full drive\directory path or replace 'strPath" with that path twice.  

Personally, I prefer assigning the path to a variable to keep the code simpler, otherwise you get something like:
   Set wb = Application.Workbooks.Open (Filename:="C:\Folder1\SubFolder2\SubSubFolder3\singlefilefolder\" + Dir("C:\Folder1\SubFolder2\SubSubFolder3\singlefilefolder\"))

nasty...

Regards,
-Glenn
0
 

Author Comment

by:Rayne
ID: 40343842
thank you Glenn :)
I did it this way

strpath = "C:myfolder\thisFolder\\"
        Set wb = Application.Workbooks.Open(strpath + Dir(strpath & "*.xlsx"))

Looks ok to you?
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40343922
1) remove the extra backslash for strpath assignment
2) Dir(strpath) should return the full filename of the single file in the folder; no need to add the "*.xlsx" part

-Glenn
0
 

Author Comment

by:Rayne
ID: 40344066
thnak you Glenn :)
I will fix it
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now