Combine 2 IF STatements

J

Junior728

Hi,

How do i make sure that only if both of the If statements are met, then the
file will be open? I tried, but somehow only the latest file will be opened,
when the filename does not start with the FName.

As below:

Not sure if it works:
Sub Test()

Const Path = "H:\My WorkStation\PRCD"
Dim FName As String
Dim FSO As Object
Dim Folder As Object
Dim strName As String
Dim oFile As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder(Path)
FName = "PRCD"

For Each x In Folder.Files
If x.DateLastModified > dteDate Then
dteDate = x.DateLastModified
strName = x.Name
End If

If UCase(x.Name) Like UCase(FName & "*" & ".xls") Then
Workbooks.Open (Path & Application.PathSeparator _
& x.Name)
End If
'Exit For
N = N + 1
Next x
MsgBox N & " files" & vbCr & strName & " _ is latest file " _
& vbCr & "Dated _ " & dteDate 'Check if it works

End Sub
 
P

papou

Hi
For Each x In Folder.Files
If x.DateLastModified > dteDate And Ucase(x.Name) Like UCase(FName & "*" &
".xls") Then
Workbooks.Open (Path & Application.PathSeparator _
& x.Name)
End If

HTH

Cordially
Pascal
 
B

Bob Phillips

Sub Test()

Const Path = "H:\My WorkStation\PRCD"
Dim FName As String
Dim FSO As Object
Dim Folder As Object
Dim strName As String
Dim oFile As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder(Path)
FName = "PRCD"

For Each x In Folder.Files
If UCase(x.Name) Like UCase(FName & "*" & ".xls") Then
If x.DateLastModified > dteDate Then
dteDate = x.DateLastModified
strName = x.Name
Workbooks.Open Path & Application.PathSeparator _
& x.Name
End If
End If
'Exit For
N = N + 1
Next x
MsgBox N & " files" & vbCr & strName & " _ is latest file " _
& vbCr & "Dated _ " & dteDate 'Check if it works

End Sub

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top