Opening Two Spreadsheets at once

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
1,004
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
Hi All,
im trying to open 2 spreadsheets one after the other. my code is:
Code:
Sub OpenFile(customer)

    If customer = "abc x" Or customer = "cde" Or customer = "zzz" Or customer = "ww" Then
        Workbooks.Open GetFile("S:\Workwear\")
    Else
    If customer = "123" Then
        Workbooks.Open GetFile("S:\Workwear\")
    Else
    If customer = "456" Then
        Workbooks.Open GetFile("S:\Workwear\")
    Else
    If customer = "789" Then
        Workbooks.Open GetFile("S:\Workwear\")
    Else
    If customer = "012" Then
        Workbooks.Open GetFile("S:\Workwear\")
    End If
    End If
    End If
    End If
    End If

End Sub
Function GetFile(Path As String) As String
    Dim f As Object, d As Date, fname1 As String, fname2 As String, loc1 As String, loc2 As String
 
    If customer = "abc x" Or customer = "cde" Or customer = "zzz" Or customer = "ww" Then
        fname1 = customer & " " & Business1 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        fname2 = customer & " " & Business2 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        loc1 = "S:\Workwear\" & fname1
        loc2 = "S:\Workwear\" & fname2
    Else
    If customer = "123" Then
        fname1 = customer & " " & Business1 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        fname2 = customer & " " & Business2 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        loc1 = "S:\Workwear\" & fname1
        loc2 = "S:\Workwear\" & fname2
    Else
    If customer = "456" Then
        fname1 = customer & " " & Business1 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        fname2 = customer & " " & Business2 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        loc1 = "S:\Workwear\" & fname1
        loc2 = "S:\Workwear\" & fname2
    Else
    If customer = "789" Then
        fname1 = customer & " " & Business1 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        fname2 = customer & " " & Business2 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        loc1 = "S:\Workwear\" & fname1
        loc2 = "S:\Workwear\" & fname2
    Else
    If customer = "012" Then
        fname1 = customer & " " & Business1 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        fname2 = customer & " " & Business2 & " OF " & WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM yy")) & ".xls"
        loc1 = "S:\Workwear\" & fname1
        loc2 = "S:\Workwear\" & fname2
    End If
    End If
    End If
    End If
    End If

    
    For Each f In CreateObject("Scripting.FileSystemObject").GetFolder(Path).Files
        If f = loc1 Then
            If d < f.DateLastModified Then
                d = f.DateLastModified
                GetFile = f.Path
            End If
        End If
    Next
    
   
    For Each f In CreateObject("Scripting.FileSystemObject").GetFolder(Path).Files
        If f = loc2 Then
            If d < f.DateLastModified Then
                d = f.DateLastModified
                GetFile = f.Path
            End If
        End If
    Next
end function
can someone tell me where im going wrong?

thanks in advance
Sam
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I haven't had a thorough look at your code, but customer won't be visible to your GetFile function, so you need to pass it as an argument, eg:

Code:
Workbooks.Open GetFile("S:\Workwear\", customer)

and the declaration becomes:

Code:
Function GetFile(Path As String, customer as String) As String
 
Upvote 0
Hi Andrew,
Thanks for your help although your suggestion doesn't work as I forgot to mention something in my OP...I do sincerely apologise for this
The reason for opening the two files is because the files are for the same customer although there are two different divisions. eg customer x, has clothing division and has footwear division.

So at the beginning of my code I define my divisions
Code:
    Business1 = "clothing"
    Business2 = "footwear"
So when I try and open the files you are right, it can find the customer with the first division, although the second division it can't find so it leaves it blank

HTH
Sam
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,822
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top