Re: Look at cell value and split file
OK, a slight change of approach is needed I think - otherwise we will be adding workbooks into the directory that is being processed and that could cause problems. The amended version below uses a function, courtesy of JWalk, to get an array of file names and then process this rather than looping through the directory.
Sub Main()
Const strPath As String = "C:\Documents and Settings\Richie\My Documents\Excel\Tests\"
'define appropriate directory here
Dim x As Variant, i As Integer
Application.ScreenUpdating = False
'speed-up process by not displaying
x = GetFileList(strPath)
Select Case IsArray(x)
Case True 'files found
For i = LBound(x) To UBound(x)
Call Subord(strPath & x(i))
'Kill strPath & x(i)
Next i
Case False 'no files found
MsgBox "No matching files"
End Select
'loop through all of the files, call a sub to do our processing, delete the original file
Application.ScreenUpdating = True
'reset display
End Sub
Function GetFileList(FileSpec As String) As Variant
'JWalk function
' Returns an array of filenames that match FileSpec
' If no matching files are found, it returns False
Dim FileArray() As Variant
Dim FileCount As Integer
Dim FileName As String
On Error GoTo NoFilesFound
FileCount = 0
FileName = Dir(FileSpec)
If FileName = "" Then GoTo NoFilesFound
' Loop until no more matching files are found
Do While FileName <> ""
FileCount = FileCount + 1
ReDim Preserve FileArray(1 To FileCount)
FileArray(FileCount) = FileName
FileName = Dir()
Loop
GetFileList = FileArray
Exit Function
' Error handler
NoFilesFound:
GetFileList = False
End Function
Sub Subord(strFullName As String)
Const strDate As String = "30/12/2004"
Dim wbk As Workbook, rngFound As Range, wbkNew As Workbook
Set wbk = Workbooks.Open(strFullName)
'open the workbook to work with
With wbk.ActiveSheet
Set rngFound = .Find(what:=DateValue(strDate), LookIn:=xlFormulas)
'search for date
If Not rngFound Is Nothing Then
Set wbkNew = Workbooks.Add
.Range("A1:V" & rngFound.Row).Cut
With wbkNew
.ActiveSheet.Paste
.SaveAs wbk.Name & "1"
.Close
End With
.Range("A1:V" & rngFound.Row).EntireRow.Delete
wbk.SaveAs wbk.Name & "2"
End If
'if date found proceed
End With
End Sub
Display More