ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Combine 2 IF STatements (https://www.excelbanter.com/excel-programming/398573-combine-2-if-statements.html)

Junior728

Combine 2 IF STatements
 
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



papou[_2_]

Combine 2 IF STatements
 
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


"Junior728" a écrit dans le message de
news: ...
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





Bob Phillips

Combine 2 IF STatements
 
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)



"Junior728" wrote in message
...
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






All times are GMT +1. The time now is 11:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com