View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
TEB2 TEB2 is offline
external usenet poster
 
Posts: 18
Default Why won't this code run??

I'm trying to loop thru all workbooks in a folder and copy the left 6 digits
of the file name from A1 to the last row with data. When I run the code it
stops on the first workbook and does nothing???

Here's the code:

Option Explicit
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


Sub PrepFiles()
Dim basebook As Workbook
Dim mybook As Workbook
Dim facility As Long
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim Lr As Long

SaveDriveDir = CurDir

MyPath = "E:\Ron Hoffman\Inpatient\Test\2003 Credit Balances"
If Right(MyPath, 1) < "\" Then
MyPath = MyPath & "\"
End If

ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xls")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

On Error GoTo CleanUp

Application.ScreenUpdating = False
Application.EnableEvents = False

Do While FNames < ""
Set mybook = Workbooks.Open(FNames, UpdateLinks:=0)
facility = Left(mybook, 6)
Lr = LastRow(Sheets("Credit Detail"))
Sheets("Credit Detail").Range("A1:A" & Lr).Value = facility

mybook.Close True
FNames = Dir()
Loop

CleanUp:
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub