Dear All,
I am having a problem trying to get the data to find the files with in the coding below, it always jumps to the else and exits the macro. Can someone please help, I need to get the coding to find a file and if it finds just 1.xls file in the folder it will open all excel files, the coding for this works fine, I just need the coding to work to find a file in the folder and check to see if the folder exists.
Sub OpenAndCopy()
Dim i As Integer
Sheets("Macro Sheet".Select
userval = Application.InputBox("Type in a unique Surname or Team Name:", "Select Team Details", 2)
If userval <> False Then [E20] = userval
findname = [E20].Value
pathname = Range("E18".Value
With Application.FileSearch
Sheets("Macro Sheet".Select
.LookIn = pathname
'* represents wildcard characters
If .Execute > 0 Then 'Workbook exists
Application.ScreenUpdating = False
Dim oFso, oFold, f1, oFiles
Dim wbkMe, wbk2 As Workbook
'ADDED
Dim lCpdRows As Long
Dim ws As String
' set up the files
Set oFso = CreateObject("Scripting.FileSystemObject"
Sheets("Macro Sheet".Select
pathname = [E18].Value
Set oFold = oFso.GetFolder(pathname) ' folder with source data
Sheets("Proficiency Vantive Cases".Select
Set oFiles = oFold.Files
Set wbkMe = ThisWorkbook
For Each f1 In oFiles
' only interested in xl files
If f1.Name Like "*.xls" Then
Set wbk2 = Workbooks.Open(Filename:=f1)
'ADDED
ws = "Proficiency Vantive Cases"
With wbk2.Worksheets("Complete Listing".Range("A4".Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
lCpdRows = Selection.Rows.Count
End With
If wbkMe.Worksheets("Proficiency Vantive Cases".Range("A65536".End(xlUp).Row _
+ lCpdRows > 65536 Then ws = "Sheet2"
With wbkMe.Worksheets(ws) 'amended
.Activate
.Range("A1".Select
' check for existing data
If IsEmpty(.Range("A1") And _
.Range("A1".End(xlDown).Row = 65536 Then _
ActiveSheet.Paste
' last row
If Not IsEmpty(.Range("A1") Then
.Range("A1".End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
End If
End With
Application.CutCopyMode = False
Application.DisplayAlerts = False
wbk2.Close
Application.DisplayAlerts = True
Set wbk2 = Nothing
End If
Next
Sheets("Proficiency Vantive Cases".Select
Rows("1:1".Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:="=Agent Names*", Operator:=xlAnd
Range("A1".Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Rows("1:1".Select
Selection.AutoFilter
Range("A1".Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("A1", Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1".Select
Columns("G:G".Select
Selection.Style = "Percent"
Selection.NumberFormat = "0.00%"
Range("A1".Select
' then close the active workbook and returns to 101 template
Application.ScreenUpdating = True
Else 'There is NOt a Workbook
MsgBox "Vantive Data Was Not Found!", vbInformation
End If
End With
Sheets("Macro Sheet".Select
End Sub
when the coding is run the .lookin = pathname does not look for the value in pathname, it looks for my desktop profile location and ignores pathname, pathname does display the correct place to look for files.
I have had this working before on 1 files in a folder, but i need it to look for any .xls files in the specified folder, and it it finds a .xls file it will open all files.
Thanks in advance for any help with this.
Rob.
I am having a problem trying to get the data to find the files with in the coding below, it always jumps to the else and exits the macro. Can someone please help, I need to get the coding to find a file and if it finds just 1.xls file in the folder it will open all excel files, the coding for this works fine, I just need the coding to work to find a file in the folder and check to see if the folder exists.
Sub OpenAndCopy()
Dim i As Integer
Sheets("Macro Sheet".Select
userval = Application.InputBox("Type in a unique Surname or Team Name:", "Select Team Details", 2)
If userval <> False Then [E20] = userval
findname = [E20].Value
pathname = Range("E18".Value
With Application.FileSearch
Sheets("Macro Sheet".Select
.LookIn = pathname
'* represents wildcard characters
If .Execute > 0 Then 'Workbook exists
Application.ScreenUpdating = False
Dim oFso, oFold, f1, oFiles
Dim wbkMe, wbk2 As Workbook
'ADDED
Dim lCpdRows As Long
Dim ws As String
' set up the files
Set oFso = CreateObject("Scripting.FileSystemObject"
Sheets("Macro Sheet".Select
pathname = [E18].Value
Set oFold = oFso.GetFolder(pathname) ' folder with source data
Sheets("Proficiency Vantive Cases".Select
Set oFiles = oFold.Files
Set wbkMe = ThisWorkbook
For Each f1 In oFiles
' only interested in xl files
If f1.Name Like "*.xls" Then
Set wbk2 = Workbooks.Open(Filename:=f1)
'ADDED
ws = "Proficiency Vantive Cases"
With wbk2.Worksheets("Complete Listing".Range("A4".Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
lCpdRows = Selection.Rows.Count
End With
If wbkMe.Worksheets("Proficiency Vantive Cases".Range("A65536".End(xlUp).Row _
+ lCpdRows > 65536 Then ws = "Sheet2"
With wbkMe.Worksheets(ws) 'amended
.Activate
.Range("A1".Select
' check for existing data
If IsEmpty(.Range("A1") And _
.Range("A1".End(xlDown).Row = 65536 Then _
ActiveSheet.Paste
' last row
If Not IsEmpty(.Range("A1") Then
.Range("A1".End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
End If
End With
Application.CutCopyMode = False
Application.DisplayAlerts = False
wbk2.Close
Application.DisplayAlerts = True
Set wbk2 = Nothing
End If
Next
Sheets("Proficiency Vantive Cases".Select
Rows("1:1".Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:="=Agent Names*", Operator:=xlAnd
Range("A1".Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Rows("1:1".Select
Selection.AutoFilter
Range("A1".Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Sort Key1:=Range("A1", Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A1".Select
Columns("G:G".Select
Selection.Style = "Percent"
Selection.NumberFormat = "0.00%"
Range("A1".Select
' then close the active workbook and returns to 101 template
Application.ScreenUpdating = True
Else 'There is NOt a Workbook
MsgBox "Vantive Data Was Not Found!", vbInformation
End If
End With
Sheets("Macro Sheet".Select
End Sub
when the coding is run the .lookin = pathname does not look for the value in pathname, it looks for my desktop profile location and ignores pathname, pathname does display the correct place to look for files.
I have had this working before on 1 files in a folder, but i need it to look for any .xls files in the specified folder, and it it finds a .xls file it will open all files.
Thanks in advance for any help with this.
Rob.