View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Bob Kilmer Bob Kilmer is offline
external usenet poster
 
Posts: 280
Default Test if Sheet Exists - Tom Ogilvy

'maybe a text file

Sub OpenFiles_New()
...
Call LogToFile(wkbk.Name & " does not have the TSData sheet")
...
End Sub

Private Sub LogToFile(msg As String)
Dim f As Integer
Dim path As String
path = "C:\"
f = FreeFile
Open path & "\MyOutput.txt" For Append As #f
Print #f, msg
Close #f
End Sub

This is convenient, but it opens and close the file for every write. If you
need more speed, store the names in ram, open the file, write them out,
close the file. This will only be an issue for huge numbers of writes
(10's - 100's of thousands or more), IMHO

Bob.

"Steph" wrote in message
...
Hi Tom,

Remember last night you modified some code for me to include a message box
for files that did not contain the sheet "timesheet" (code below). Is it
possible to write the files that do not have that sheet to a log (maybe a
text file, or even the immediate window) as well as the message box? I

was
hoping to add some code that would kick off this procedure automatically,
and didn't want the message box to hold up the procedure waiting for the

ok
click.

Thanks in advance!


Sub OpenFiles_New()
'Opens Files in Folder

Dim GetFiles As Variant
Dim iFiles As Long
Dim nFiles As Long
Dim wkbk As Workbook
Dim sh As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False
GetFiles = Application.GetOpenFilename _
(FileFilter:="Text Files (*.*),*.*", _
Title:="Select Timesheets to Include in SAP PO Upload",
MultiSelect:=True)
If TypeName(GetFiles) = "Boolean" Then
MsgBox "No Files Selected", vbOKOnly, "Nothing Selected"
End
Else
For iFiles = LBound(GetFiles) To UBound(GetFiles)
Workbooks.OpenText Filename:=GetFiles(iFiles)
Set wkbk = ActiveWorkbook

Set sh = Nothing
On Error Resume Next
Set sh = wkbk.Worksheets("TSData")
On Error Resume Next
If Not sh Is Nothing Then

' With ActiveWorkbook.Sheets("Timesheet").UsedRange
' .Value = .Value
' End With

wkbk.Sheets("TSData").Range("A10:AG" & _
Sheets("Timesheet").Range("A20").End(xlUp).Row).Co py
ThisWorkbook.Worksheets("Consol").Range("A" & _
Consol.Range("E65536").End(xlUp).Offset(1,

0).Row).PasteSpecial
_
Paste:=xlPasteValues
Else
MsgBox wkbk.Name & " does not have the TSData sheet"
End If
wkbk.Close
Next iFiles
End If

'**********************
'Duplicate Test Here

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub