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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
For i = 3 To lastRow
wsReimbursement.Cells(i, 8).Font.Color = vbBlack
wsReimbursement.Cells(i, 9).Font.Color = vbBlack
cellValue = Trim(wsReimbursement.Cells(i, 2).Text)
If InStr(Left(cellValue, 5), ".") > 0 And IsNumeric(Replace(Left(cellValue, 5), ".", "0", 1, 1)) Then
extractedPrefix = Left(cellValue, 5)
ElseIf InStr(Left(cellValue, 4), ".") > 0 And IsNumeric(Replace(Left(cellValue, 4), ".", "0", 1, 1)) Then
extractedPrefix = Left(cellValue, 4)
ElseIf InStr(Left(cellValue, 3), ".") > 0 And IsNumeric(Replace(Left(cellValue, 3), ".", "0", 1, 1)) Then
extractedPrefix = Left(cellValue, 3)
Else
extractedPrefix = wsReimbursement.Range("F1").value
End If
If wsReimbursement.Cells(i, 6).value <> "" Then
invoiceNumbers = Split(wsReimbursement.Cells(i, 6).value, " ")
totalInvoiceAmount = 0
invoiceName = ""
formulaStr = ""
For Each invoiceNum In invoiceNumbers
If Trim(invoiceNum) <> "" Then
Set foundInvoiceRow = Nothing
Set foundInvoiceRow = wsInvoice.Range("C:C").Find(Trim(invoiceNum), LookIn:=xlValues, LookAt:=xlWhole)
If Not foundInvoiceRow Is Nothing Then
Dim invoiceAmount As Double
invoiceAmount = foundInvoiceRow.Offset(0, 6).value
Dim invoiceFileName As String
invoiceFileName = """" & extractedPrefix & "-" & invoiceAmount & "-" & Trim(invoiceNum) & ".pdf" & """"
If invoiceName <> "" Then
invoiceName = invoiceName & " " & invoiceFileName
Else
invoiceName = invoiceFileName
End If
totalInvoiceAmount = totalInvoiceAmount + invoiceAmount
If formulaStr = "" Then
formulaStr = "=" & invoiceAmount
Else
formulaStr = formulaStr & "+" & invoiceAmount
End If
Else
wsReimbursement.Cells(i, 6).Font.Color = vbMagenta
Debug.Print "第 " & i & " 行的发票号未能找到: " & Trim(invoiceNum)
End If
End If
Next invoiceNum
wsReimbursement.Cells(i, 9).value = invoiceName
If formulaStr <> "" Then
wsReimbursement.Cells(i, 10).Formula = formulaStr
End If
If invoiceName <> "" Then
allFilesFound = True
Dim files As Variant, F As Variant
files = Split(invoiceName, " ")
For Each F In files
Dim trimmedFile As String
trimmedFile = Replace(Trim(F), """", "")
If trimmedFile <> "" Then
' 【重构点】: 调用本模块内的私有 FileExists 函数
If Not FileExists(ATTACHMENTS_FOLDER & "\" & trimmedFile) Then
allFilesFound = False
Exit For
End If
End If
Next F
If Not allFilesFound Then
wsReimbursement.Cells(i, 9).Font.Color = vbRed
End If
End If
' 判断是否有非发票的差额
If wsReimbursement.Cells(i, 10).value < wsReimbursement.Cells(i, 1).value Then
nonInvoiceFile = """" & extractedPrefix & "-" & (wsReimbursement.Cells(i, 1).value - wsReimbursement.Cells(i, 10).value) & ".jpg" & """"
wsReimbursement.Cells(i, 8).value = nonInvoiceFile
If Not FileExists(ATTACHMENTS_FOLDER & "\" & Replace(nonInvoiceFile, """", "")) Then
wsReimbursement.Cells(i, 8).Font.Color = vbRed
End If
End If
Else ' 如果完全没有发票号
nonInvoiceFile = """" & extractedPrefix & "-" & wsReimbursement.Cells(i, 1).value & ".jpg" & """"
wsReimbursement.Cells(i, 8).value = nonInvoiceFile
If Not FileExists(ATTACHMENTS_FOLDER & "\" & Replace(nonInvoiceFile, """", "")) Then
wsReimbursement.Cells(i, 8).Font.Color = vbRed
End If
End If
Next i
|