Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking that an external file is open 1

Status
Not open for further replies.

LGMan

MIS
Aug 27, 2003
233
0
0
GB
Hi, I get sent a file each day called 'Daily_Sales_ddmmyy'.xlsb, so todays was called Daily_Sales_140218.xlsb
The code below loops though ther open workbooks until it finds the Daily_Sales file, I know to have that file open before I trigger the Procedure which pulls this file into my Master Workbook where the VBA is stored. As the Workbook Name changes each day, I've gone for the approach below as I can't hard code the Daily File name into the workbook.

Code:
Dim DHFile as Workbook 'Daily File
For Each wb in Application.Workbooks
[indent]If wb.Name Like "Daily Sales*" Then[/indent]
[indent]Set DHFile = wb[/indent]
[indent]End If[/indent]
Next wb
What could I add as a fail-safe incase the Daily File isn't open,
I've tried
Code:
If wb Is Nothing then Msgbox" The Daily Sales file must be opened before continuing":Exit Sub
but this runs to the Exit Sub even if the Daily_Sales file is open.

Thanks for any help
 
Hi
Code:
If wb.Name Like "Daily_Sales*" Then

Don’t forget the UNDERSCORE character.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Hi Skip, I had to type this out manually, the code in my workbook runs as all the names are correct
 
You assume that we know what’s after the code you posted.

This is not the way I would do it.

1) you know the date so you know the name of todays’s file
“Daily_Sales_” & Format(Date, “ddmmyy”) & “.xlsb”

2) IMPORT the data via MS Query. Set up the QueryTable ONE TIME, and the programsticslly change the Connection string and refresh the query. Then copy the table to whereever in your workbook.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Testing for [tt]Nothing[/tt]:
Code:
If [COLOR=#CC0000][b]DHFile[/b][/color] Is Nothing then Msgbox" The Daily Sales file must be opened before continuing":Exit Sub


combo
 
Good call, combo! Missed that one.

Gotta watch those shuffling shells carefully ;-)

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Thanks Combo, it works like I hoped it achieve in the first place!


Skip, I take yout point that in this ocassion I would be able to use the day and use it in the filename, but there are temporary files that I download from the company IntranNet and they sometimes for reasons unknown have a load of guff added at the end of the filename.
Hence I wanted some code to apply to several data scrape processes. The trouble was that I should have said that in the first place. However your advice is good as always
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top