施工照片VBA-常見問題

前言

自從施工照片VBA開始實際投入報表製作過程後,陸陸續續有些使用回饋心得,其中有相當多的常見問題我想要撰寫於這篇文章中,讓各位能有個來源解除這些疑惑,畢竟工具很難完美,肯定是需要經過大量的操作,才會讓他變得更好!

如果使用上還有些什麼更好的建議,請透過左側聯繫我~

資料夾照片順序與檔案順序不一致

從LINE相簿下載的照片名稱雖然上面有順序,但是匯入施工照片VBA之後卻不會按照原本的順序進行排列?

Fig1. 原本的順序是(1).(2).(3)...(10)...(100)...
Fig2. 當引入之後會變成(1).(10).(100)...

發生原因

對於數字而言,1.2.3...10.11...100.101...這樣排序會正常運作,但是在excel中,名稱所有的數字都會變成文字來處理,所以1的後面是10,再來是100

解決方案

把原本是數字的部分通通變成3碼,001.002.003.....010.011.....100,他就會照這樣排序上去了。

可想而知,如果各位要這樣一筆一筆手動修改也是很累人的一件事,所以此時就可以寫個VBA工具將檔案名稱做關鍵字拆分,取出最後的編號部分,用格式化選項作成三碼的數字,重新併回去產製新的照片名稱。

一般來說,LINE是存放檔案的最大宗,從相簿下載回電腦的名稱為LINE_ALBUN_相簿名稱_相簿日期_流水號.jpg,此時就能透過這個方式先將流水號拆出來,再將流水號修改為固定三位數的流水號。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Sub renameFiles()
Dim main_path As String
Dim fileName As String
Dim newFileName As String
Dim pos As Long
Dim posDot As Long
Dim numPart As String
Dim ext As String

' 取得資料夾路徑
main_path = Sheets("Main").Range("B2").Value

If Right(main_path, 1) <> "\" Then
main_path = main_path & "\"
End If

fileName = Dir(main_path & "*.*")

Do While fileName <> ""
pos = InStrRev(fileName, "_") ' 找到最後一個 "_"
posDot = InStrRev(fileName, ".") ' 找到最後一個 "."

If pos > 0 And posDot > pos Then

numPart = mid(fileName, pos + 1, posDot - pos - 1)

If IsNumeric(numPart) Then

numPart = Format(CLng(numPart), "000")

newFileName = Left(fileName, pos) & numPart & mid(fileName, posDot)

Name main_path & fileName As main_path & newFileName

Debug.Print "Rename: " & fileName & " -> " & newFileName
End If
End If

fileName = Dir
Loop

MsgBox "重新編號完成!", vbInformation
End Sub

相關連結

施工照片VBA-實作流程