如果使用不一致的字符串,可能會非常棘手。這是一個函數,可以確定匹配的姓氏和名字的首字母,前提是字符串模式不會超出您的示例。將它添加到模塊中,然後通過在單元格中鍵入公式= AppendFileName來訪問它。
Public Function AppendFileName(ByVal LName As String, ByVal FName As String, ByVal FileName As String, ByVal AccN As String) As String
If LName = Left(FileName, Len(LName)) Then
If Mid(FileName, Len(LName) + 1, 1) = "," Then 'Check if the string contains a comma
If Mid(FileName, Len(LName) + 3, 1) = Left(FName, 1) Then
AppendFileName = FileName & " " & AccN
End If
Else 'If no comma then assume just one space
If Mid(FileName, Len(LName) + 2, 1) = Left(FName, 1) Then
AppendFileName = FileName & " " & AccN
End If
End If
End If
If AppendFileName = "" Then AppendFileName = False
End Function
您可以創建一個循環解決此代碼要經過的所有文件和名稱,並與DIR功能自動化,例如。
Dim x as integer, DirFile as string
DirFile = Dir(Path)
Do While DirFile <> ""
x = x + 1 'To track how many files, and to assign variables as in below line of code
'Set each string variable like FName = Range("A1").offset(x,0).value
'Then assess the the names and file names with the If statements above
'Do something with appended FileName
DirFile = Dir
Loop
希望這會有所幫助。
可能是值得的[Excel的VBA]標籤 –
你會怎樣想對付的'史密斯,J'爲'約翰Smith'和'傑克·史密斯' –