How to validate path? --- bValidatePath ("") Then

  • Thread starter Thread starter dim
  • Start date Start date
D

dim

Hi all,

I have two workbooks called Book1 and Book2. Book1 simply displays a
userform when opened, and when the user clicks 'Ok' it closes Book1 and opens
Book2.

I am going to create a 3rd workbook called Book3. Book3 will be an exact
copy of Book2, but in Book2 all VBA code path's read C:\.... and in Book3 I
will have them reading D:\...

This will be to allow a user to run the program from a hard-drive designated
as C or D. The files are all located in a folder called My Program, and the
user's install instructions are to copy the folder into their Program Files
path. So the total path on the users computer will end up being either:

C:\Program Files\My Program\Book2.xls
or
D:\Program Files\My Program\Book3.xls

When Book1 is open, I want to include in the Button1 Click macro some form
of validation so it can tell if it is located in C:\Program Files\... or
D:\Program Files\.... This will be followed by an IF THEN ELSE, so that if
its in C:\... Then it will open Book2, and if its in D:\... it will open
Book3....makes sense?...any ideas how to get this to work?

I think the code involves 'ValidatePath' something something?

Thankyou.
 
Hi all,

I have two workbooks called Book1 and Book2. Book1 simply displays a
userform when opened, and when the user clicks 'Ok' it closes Book1 and opens
Book2.

I am going to create a 3rd workbook called Book3. Book3 will be an exact
copy of Book2, but in Book2 all VBA code path's read C:\.... and in Book3 I
will have them reading D:\...

This will be to allow a user to run the program from a hard-drive designated
as C or D.  The files are all located in a folder called My Program, andthe
user's install instructions are to copy the folder into their Program Files
path. So the total path on the users computer will end up being either:

C:\Program Files\My Program\Book2.xls
or
D:\Program Files\My Program\Book3.xls

When Book1 is open, I want to include in the Button1 Click macro some form
of validation so it can tell if it is located in C:\Program Files\... or
D:\Program Files\.... This will be followed by an IF THEN ELSE, so that if
its in C:\... Then it will open Book2, and if its in D:\... it will open
Book3....makes sense?...any ideas how to get this to work?

I think the code involves 'ValidatePath' something something?

Thankyou.
Will this work for you?

Sub validatepath()
If Dir("C:\Program Files\My Program\") <> "" Then
Workbooks.Open Filename:= _
"C:\Program Files\My Program\Book2.xls"
Else
If Dir("D:\Program Files\My Program\") <> "" Then
Workbooks.Open Filename:= _
"D:\Program Files\My Program\Book3.xls"
End If
End If
End Sub
 
Hi

Try
Sub test()
If Left(ActiveWorkbook.Path, 1) = "C" Then
MsgBox ("Load Book2")
ElseIf Left(ActiveWorkbook.Path, 1) = "D" Then
MsgBox ("Load Book3")
End If
End Sub

Substitute your load statements in place of MsgBox in each case.
 
Hi,

GTV, I didn't try your way, because if the user has the files installed in a
harddrive designated 'D', then I think your first If statement would return
an error since there may be no C drive present at all. But thankyou.

Roger, I tried you way and thus far it's working, but I haven't transferred
the files onto the other computer with the D drive to test it yet, but it
executes fine on my C drive with the code inserted for both.

Thankyou both for your help. Its appreciated.
 
Hi,

GTV, I didn't try your way, because if the user has the files installed ina
harddrive designated 'D', then I think your first If statement would return
an error since there may be no C drive present at all. But thankyou.

Roger, I tried you way and thus far it's working, but I haven't transferred
the files onto the other computer with the D drive to test it yet, but it
executes fine on my C drive with the code inserted for both.

Thankyou both for your help. Its appreciated.

on mine if there was no C: then it would then go and look for D: Roger
and I basically have the same code except mine is looking for the
complete path and his is looking for just the drive letter.
 
You would not get an error. Dir(path) would simply return "" if the path
being sought were not available.

- Jon
 
Actually, a better test than

If SomeString = ""

is

If Len(SomeString) = 0

Because it's quicker to determine the length of one string than to compare
the values of two strings.

- Jon
 
Back
Top