您似乎在思考正确的方向。重构可能是一个好主意,因为方法非常相似。试试这个:
' FlipMethod cases handled:
' If "FN LN to LN, FN" is supplied: John Smith will be converted to Smith, John
' If "LN, FN to FN LN" is supplied: Smith, John will be converted to John Smith
Sub FlipNames(FlipMethod as String) 'FN LN to LN, FN
'Purpose: Converts selected cells First Name Last Name in place to Last Name, First Name
Dim x As Integer
Dim sCell As String
Dim sLast As String
Dim sFirst As String
Dim rCell As Range
For Each rCell In Selection 'sets range to selection
sCell = rCell.Value
if FlipMethod = "FN LN to LN, FN" then
x = InStr(sCell, " ") 'searches for space
else
x = Instr(sCell, ",") ' searches for comma
end if
If x > 0 Then 'flips order
sFirst = Left(sCell, x - 1)
sLast = Mid(sCell, x + 1)
if FlipMethod = "FN LN to LN, FN" then
rCell.Value = sLast & ", " & sFirst 'places comma in between LN, FN
else
rCell.Value = sLast & " " & sFirst 'places space in between FN LN
rCell.Value = LTrim(rCell) 'trims off leading spaces
end if
End If
Next
Set rCell = Nothing 'resets the range to zero
End Sub
有可能是在多个细胞中分离名称格式和迭代功能的一些价值。以下是一个与迭代分离的功能示例。
Enum NameFormat
FNLN_TO_LNFN_WITH_COMMA = 1
LNFN_WITH_COMMA_TO_FNLN = 2
End Enum
Function FlipNames(Data As String, NameFormat As Long) As String
Dim x As Integer
Dim sLast As String
Dim sFirst As String
' Exit early if data is improper
If IsNull(Data) Or Len(Trim(Data)) = 0 Then
FlipNames = Data
Exit Function
End If
' Check if comma or space is present, depending on requirements
Select Case (NameFormat)
Case FNLN_TO_LNFN_WITH_COMMA
x = InStr(Data, " ")
Case LNFN_WITH_COMMA_TO_FNLN
x = InStr(Data, ",")
Case Else
FlipNames = Data
Exit Function
End Select
' Exit early if required split character not found
If x <= 0 Then
FlipNames = Data
Exit Function
End If
' Find first and last names
sFirst = Trim(Left(Data, x - 1))
sLast = Trim(Mid(Data, x + 1))
' Put data together as desired
Select Case NameFormat
Case FNLN_TO_LNFN_WITH_COMMA
FlipNames = sLast & ", " & sFirst
Case LNFN_WITH_COMMA_TO_FNLN
FlipNames = Trim(sLast & " " & sFirst)
End Select
End Function
当有需要添加更多的功能
- 添加枚举常数以指示样是需要
- 添加代码,以分割的数据格式的
- 添加代码以重新加入data
此外,您可以添加测试用例以确保此函数能够满足dif不同的传入数据。您可以编写测试,像这样:
Sub Test_FlipNames()
Dim TestCase As String
Dim ExpectedResult As String
Dim Result As String
TestCase = "John Smith"
ExpectedResult = "Smith, John"
Result = FlipNames(TestCase, NameFormat.FNLN_TO_LNFN_WITH_COMMA)
Test_PrintResults TestCase, ExpectedResult, Result
TestCase = "John Smith"
ExpectedResult = TestCase
Result = FlipNames(TestCase, 1000)
Test_PrintResults TestCase, ExpectedResult, Result
TestCase = "Smith, John"
ExpectedResult = "John Smith"
Result = FlipNames(TestCase, NameFormat.LNFN_WITH_COMMA_TO_FNLN)
Test_PrintResults TestCase, ExpectedResult, Result
TestCase = "Smith, John"
ExpectedResult = TestCase
Result = FlipNames(TestCase, 1000)
Test_PrintResults TestCase, ExpectedResult, Result
TestCase = "John"
ExpectedResult = "John"
Result = FlipNames(TestCase, NameFormat.FNLN_TO_LNFN_WITH_COMMA)
Test_PrintResults TestCase, ExpectedResult, Result
TestCase = "John"
ExpectedResult = "John"
Result = FlipNames(TestCase, NameFormat.LNFN_WITH_COMMA_TO_FNLN)
Test_PrintResults TestCase, ExpectedResult, Result
End Sub
Sub Test_PrintResults(TestCase As String, ExpectedResult As String, Result As String)
Debug.Print "Case: " & TestCase & "; Expected: " & ExpectedResult
Debug.Print IIf(Result = ExpectedResult, "PASS", "FAILED")
End Sub
这种测试的好处是,如果该功能的变化,现有的测试,可以运行,以确保以前的功能还没有打破。然后添加更多测试以检查添加的其他代码是否正确。
要调用该函数在一个单元格范围,你可以像你有它的方式:
Sub FlipNamesInSelection()
Dim rCell as Range
For Each rCell in Selection
rCell.Value = FlipNames(rCell.Value, NameFormat.LNFN_WITH_COMMA_TO_FNLN)
Next
End Sub
下面的答案是否适合您的问题?如果是这样,我建议标记答案之一作为接受关闭你的问题。 – zedfoxus