Activating Workbooks...

D

Dennis

I'm creating a VB macro that needs to activate multiple
workbooks that all have a different name.

I.E.

workbook 1 = 91547.645
workbook 2 = 93284.670
workbook 3 = 91549.680

where the first five characters are the fund number, and
the last three characters are the workstation number

I don't want the macro to care which workstation the fund
is on, and just activate the workbook based on the fund
number... I got the workbooks to open using fundnumber
& ".*"... but I can't activate or close the workbooks by
using the ".*" Anyone know a solution?

Thanks

-Dennis
 
G

Guest

hi,
no. no solution. you will have to use the full workbook
title for activate and close.
 
D

Dave Peterson

Maybe you could just look for a match in the prefix:

Option Explicit
Function CloseByPrefix(prefixName As String, _
Optional SaveIt As Boolean = False) As Boolean

Dim wkbk As Workbook

CloseByPrefix = False
For Each wkbk In Application.Workbooks
If LCase(Left(wkbk.Name, Len(prefixName))) = LCase(prefixName) Then
CloseByPrefix = True
wkbk.Close savechanges:=SaveIt
Exit For
End If
Next wkbk

End Function

Sub testme()
Dim ClosedOk As Boolean
ClosedOk = CloseByPrefix(prefixName:="98765.", SaveIt:=True)

If ClosedOk Then
'do something
Else
'do something else
End If

'I'll just use msgbox
MsgBox ClosedOk

End Sub
 
Top