View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Do While only does once

I am not sure why you get a type mismatch, but you never change the variable
file in your loop, so it it is entered, it should loop forever.

Dim OldName As String
Dim NewName As String
Dim File
Dim myPath As String

myPath = ActiveWorkbook.Path
File = Dir(myPath & "\*.WMV")

Do While File < ""
'Application.StatusBar = "Renaming files."
If File < ThisWorkbook.Name Then
OldName = File
' MsgBox OldName
' MsgBox NewName

NewName = Application.VLookup(OldName, Range("sortdata"), 4,
False)

POldName = myPath & "\" & OldName
PNewName = myPath & "\" & NewName

MsgBox POldName
MsgBox PNewName

Name POldName As PNewName
' OldName = ""
' NewName = ""
End If
file = Dir() ' get the next filename
'Exit Sub
Loop


However, it isn't advisable to change your directory while doing a loop
based on Dir. This can cause problems. It is better to gather your file
list to an array in the Dir loop, then loop through the array making your
changes.

--
Regards,
Tom Ogilvy

"Mike Williams" wrote in message
...
I'm having trouble getting a do loop to advance. I'm used to working
with for next loops, and this one is new to me. I'm trying to cycle
through files in a folder, look their names up in a table, find a new
name, and rename the file. This code works on the first iteration, and
then errors out the second time through on the Name line, with a type
mismatch. Any suggestions would be greatly appreciated. THANKS.


Dim OldName As String
Dim NewName As String
Dim File
Dim myPath As String

myPath = ActiveWorkbook.Path
File = Dir(myPath & "\*.WMV")

Do While File < ""
'Application.StatusBar = "Renaming files."
If File < ThisWorkbook.Name Then
OldName = File
' MsgBox OldName
' MsgBox NewName

NewName = Application.VLookup(OldName, Range("sortdata"), 4,
False)

POldName = myPath & "\" & OldName
PNewName = myPath & "\" & NewName

MsgBox POldName
MsgBox PNewName

Name POldName As PNewName
' OldName = ""
' NewName = ""
End If
'Exit Sub
Loop