我下载了银行交易历史记录,想提取位于描述行末尾的客户姓名。有些行在客户姓名前面有一个或两个单词。我该如何提取全部大写的名字。
我还需要从文本中提取转账金额并将其转换为数字。
DESCRIPTION AMOUNT
TRSF E-BANKING DB 2701/FTSCY/WS95051 12000.00 JAMES BOND 12,000.00 DB
TRSF E-BANKING CR 2701/FTSCY/WS95051 10000.00 deposit CHRISTINE 10,000.00 CR
TRSF E-BANKING CR 2701/FTSCY/WS95051 25025.00 AMANDA B GREEN 25,025.00 CR
TRSF E-BANKING CR 2701/FTSCY/WS95051 5000.00 msn1888 JOSH BROWN 5,000.00 CR
TRSF E-BANKING CR 2701/FTSCY/WS95051 1000.00 topup CHRISTINE 1,000.00 CR
结果:
CUSTOMER TRANSFER
JAMES BOND 12,000
CHRISTINE 10,000
AMAND B GREEN 25,025
JOSH BROWN 5,000
CHRISTINE 1,000
TOTAL 53,025
答案1
使用宏/VBA:
Public Function extract_name(transaction As String)
Dim WordArray() As String
WordArray() = Split(transaction, " ")
firstName = WordArray(5)
extract_name = firstName
extract_name_uc = UCase(extract_name)
If extract_name = extract_name_uc Then
topBound = 5
Else
topBound = 6
End If
extract_name = ""
For i = topBound To UBound(WordArray)
tempValue = WordArray(i)
If IsNumeric(tempValue) Then
i = UBound(WordArray)
Else
extract_name = extract_name & " " & tempValue
End If
Next i
extract_name = Trim(extract_name)
End Function
Public Function extract_amount(transaction As String)
Dim WordArray() As String
WordArray() = Split(transaction, " ")
extract_amount = WordArray(UBound(WordArray) - 1)
End Function
有两个函数extract_name
和extract_amount
。
使用 ALT+ F11 打开 VBA,在下面插入一个模块本工作簿并将代码粘贴到右侧。
假设TRSF E-BANKING DB 2701/FTSCY/WS95051 12000.00 JAMES BOND 12,000.00 DB
在牢房里A2然后:
B2 =extract_name(A2)
和C2 =extract_amount(A2)
。