Check the cells in unchanged workbook and give me the exact contents in the cell.
It is working just fine here.
Sub test()
Dim myDir As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then myDir = .SelectedItems(1)
End With
If myDir = vbNullString Then Exit Sub
ReNameFiles myDir
End Sub
Sub ReNameFiles(ByVal myDir As String)
Dim sfo As Object, myFile As Object, myFolder As Object, temp As String, newName, i As Long
Set sfo = CreateObject("Scripting.FileSystemObject")
For Each myFile In sfo.GetFolder(myDir).Files
If myFile.Name Like "*_A_*" Then
For i = 4 To 5
newName = ExecuteExcel4Macro("right(substitute('" & myDir & "\[" & myFile.Name & "]sheet1'!r" & i & "c1,""/"",""""),8)")
If (Not IsError(newName)) Then
If (newName <> vbNullString) * (IsNumeric(newName)) Then Exit For
End If
Next
If IsError(newName) Then newName = GetDate(myDir & "\" & myFile.Name)
If newName <> "" Then
newName = Replace(newName, "/", "")
temp = Left$(sfo.GetBaseName(myFile.Name), 10) & newName & _
"." & sfo.GetExtensionName(myFile.Name)
Name myDir & "\" & myFile.Name As myDir & "\" & temp
End If
newName = ""
End If
Next
For Each myFolder In sfo.GetFolder(myDir).SubFolders
ReNameFiles myFolder.Path
Next
End Sub
Function GetDate(fn As String) As String
Dim txt As String
txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
With CreateObject("VBScript.RegExp")
.Pattern = "Transaction Date .+to ([^<]+)"
If .test(txt) Then GetDate = .Execute(txt)(0).submatches(0)
End With
End Function
Sub Test()
Dim myDir As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then myDir = .SelectedItems(1)
End With
If myDir = vbNullString Then Exit Sub
ReSaveFiles myDir
'MsgBox ("Done Part 1")
ReNameFilesOriginal myDir
'MsgBox ("Done Part 2")
End Sub
Sub ReSaveFiles(ByVal myDir As String)
Dim sfo As Object, MyFile As Object, myFolder As Object, temp As String, newName, i As Long, filestr As String
Dim daily_wk As Workbook
Set sfo = CreateObject("Scripting.FileSystemObject")
For Each MyFile In sfo.GetFolder(myDir).Files
If MyFile.Name Like "*_A_*" Then
Filename = myDir + "\" + MyFile.Name
Workbooks.Open Filename
Windows(MyFile.Name).Activate
ActiveWorkbook.Save
ActiveWorkbook.Close Savechanges:=True
'MsgBox (Filename)
End If
Next
'Stop
For Each myFolder In sfo.GetFolder(myDir).subfolders
ReSaveFiles myFolder.Path
Next
End Sub
Sub ReNameFilesOriginal(ByVal myDir As String)
Dim sfo As Object, MyFile As Object, myFolder As Object, temp As String, newName, i As Long
Set sfo = CreateObject("Scripting.FileSystemObject")
For Each MyFile In sfo.GetFolder(myDir).Files
If MyFile.Name Like "*_A_*" Then
For i = 4 To 5
newName = ExecuteExcel4Macro("right(substitute('" & myDir & "\[" & MyFile.Name & "]sheet1'!r" & i & "c1,""/"",""""),8)")
If (Not IsError(newName)) Then
If (newName <> vbNullString) * (IsNumeric(newName)) Then
temp = Left$(sfo.GetBaseName(MyFile.Name), 10) & newName & _
"." & sfo.GetExtensionName(MyFile.Name)
Name myDir & "\" & MyFile.Name As myDir & "\" & temp
Exit For
End If
End If
Next
End If
Next
For Each myFolder In sfo.GetFolder(myDir).subfolders
ReNameFilesOriginal myFolder.Path
Next
End Sub
Hi, just tried it a couple times, doesn't seem to work when the date is on either B4/B5.Try my last code again.
No effect, it must be something other than xml.What about the file comes from xml?
If the file is originally xml, it should change the file name without opening.
Sub test()
Dim myDir As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then myDir = .SelectedItems(1)
End With
If myDir = vbNullString Then Exit Sub
Application.ScreenUpdating = False
SearchFiles myDir
Application.ScreenUpdating = True
End Sub
Sub SearchFiles(ByVal myDir As String)
Dim a() As String, sfo As Object, MyFile As Object
Dim myFolder As Object, newName, r As Range, i As Long, n As Long
Set sfo = CreateObject("Scripting.FileSystemObject")
For Each MyFile In sfo.GetFolder(myDir).Files
If MyFile.Name Like "*_A_*" Then
With Workbooks.Open(myDir & "\" & MyFile.Name).Sheets("sheet1")
.Parent.Save
For Each r In .Range("a4:b5")
newName = Right$(Replace(r.Value, "/", ""), 8)
If (newName <> "") * (IsNumeric(newName)) Then
n = n + 1
ReDim Preserve a(1 To 2, 1 To n)
a(1, n) = myDir & "\" & MyFile.Name
a(2, n) = myDir & "\" & Left$(sfo.GetBaseName(MyFile.Name), 10) & _
newName & "." & sfo.GetExtensionName(MyFile.Name)
Exit For
End If
Next
.Parent.Close False
End With
End If
Next
For Each myFolder In sfo.GetFolder(myDir).subfolders
SearchFiles myFolder.Path
Next
If n > 0 Then
For i = 1 To n
Name a(1, i) As a(2, i)
Next
End If
End Sub
Code:Sub test() Dim myDir As String With Application.FileDialog(msoFileDialogFolderPicker) If .Show Then myDir = .SelectedItems(1) End With If myDir = vbNullString Then Exit Sub Application.ScreenUpdating = False SearchFiles myDir Application.ScreenUpdating = True End Sub Sub SearchFiles(ByVal myDir As String) Dim a() As String, sfo As Object, MyFile As Object Dim myFolder As Object, newName, r As Range, i As Long, n As Long Set sfo = CreateObject("Scripting.FileSystemObject") For Each MyFile In sfo.GetFolder(myDir).Files If MyFile.Name Like "*_A_*" Then With Workbooks.Open(myDir & "\" & MyFile.Name).Sheets("sheet1") .Parent.Save For Each r In .Range("a4:b5") newName = Right$(Replace(r.Value, "/", ""), 8) If (newName <> "") * (IsNumeric(newName)) Then n = n + 1 ReDim Preserve a(1 To 2, 1 To n) a(1, n) = myDir & "\" & MyFile.Name a(2, n) = myDir & "\" & Left$(sfo.GetBaseName(MyFile.Name), 10) & _ newName & "." & sfo.GetExtensionName(MyFile.Name) Exit For End If Next .Parent.Close False End With End If Next For Each myFolder In sfo.GetFolder(myDir).subfolders SearchFiles myFolder.Path Next If n > 0 Then For i = 1 To n Name a(1, i) As a(2, i) Next End If End Sub
Name a(1,i) As a(2,i) &
Hi, jindon
The follow code at the bottom of the vba receives an Error 53: File not found, but all the files are renamed.
Code:Name a(1,i) As a(2,i)
Sub test121716()
Dim myDir As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then myDir = .SelectedItems(1)
End With
If myDir = vbNullString Then Exit Sub
'Application.ScreenUpdating = False
SearchFiles myDir
'Application.ScreenUpdating = True
'Call Shell("explorer.exe" & " " & myDir, vbNormalFocus)
End Sub
Sub SearchFiles(ByVal myDir As String)
Dim a() As String, sfo As Object, MyFile As Object
Dim myFolder As Object, newName, r As Range, i As Long, n As Long
Set sfo = CreateObject("Scripting.FileSystemObject")
For Each MyFile In sfo.GetFolder(myDir).Files
If MyFile.Name Like "NCIR*" Then
With Workbooks.Open(myDir & "\" & MyFile.Name).Sheets("sheet1")
.Parent.Save
For Each r In .Range("a4:b5")
newName = Right$(Replace(r.Value, "/", ""), 8)
If (newName <> "") * (IsNumeric(newName)) Then
n = n + 1
'ReDim Preserve a(1 To 2, 1 To n)
'''Test
If MyFile.Name Like "*_A_*" Then
ReDim Preserve a(1 To 2, 1 To n)
a(1, n) = myDir & "\" & MyFile.Name
a(2, n) = myDir & "\" & Left$(sfo.GetBaseName(MyFile.Name), 10) & _
newName & "." & sfo.GetExtensionName(MyFile.Name)
End If
If MyFile.Name Like "*_YMT_20*" Then
ReDim Preserve a(1 To 2, 1 To n)
a(1, n) = myDir & "\" & MyFile.Name
a(2, n) = myDir & "\" & Left$(sfo.GetBaseName(MyFile.Name), 14) & _
newName & "." & sfo.GetExtensionName(MyFile.Name)
End If
If MyFile.Name Like "*_YMT_*_*_*" Then
ReDim Preserve a(1 To 2, 1 To n)
a(1, n) = myDir & "\" & MyFile.Name
a(2, n) = myDir & "\" & Left$(sfo.GetBaseName(MyFile.Name), 16) & _
newName & "." & sfo.GetExtensionName(MyFile.Name)
End If
'''Test
Exit For
End If
Next
.Parent.Close False
End With
End If
Next
For Each myFolder In sfo.GetFolder(myDir).subfolders
SearchFiles myFolder.Path
Next
If n > 0 Then
For i = 1 To n
Name a(1, i) As a(2, i)
Next
End If
End Sub