2016-11-26 2 views
0

人の名前のレイアウトを変更する2つのサブプロシージャがあります。最初の名前は名字と姓の間のスペースを探すことで名字に変更されます。 (:姓、名を元)とバック名姓の順に、それを反転させ2つのsimular Subプロシージャを結合する方法

Sub FlipNames() '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 
     x = InStr(sCell, " ")  'searches for space 
     If x > 0 Then    'flips order 
      sFirst = Left(sCell, x - 1) 
      sLast = Mid(sCell, x + 1) 
      rCell.Value = sLast & ", " & sFirst 'places comma in between LN, FN 
     End If 
    Next 
    Set rCell = Nothing    'resets the range to zero 
End Sub 

二SUP手順は、2名の間に「」カンマを探します。私はとの助けをしたいと思い何

Sub FlipNames2() 'LN, FN to FN LN 
'Purpose: Converts selected cells Last Name, First Name in place to First Name Last 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 
     x = InStr(sCell, ",")  'searches for comma 
     If x > 0 Then    'flips order 
      sFirst = Left(sCell, x - 1) 
      sLast = Mid(sCell, x + 1) 
      rCell.Value = sLast & " " & sFirst 'places space in between FN LN 
      rCell.Value = LTrim(rCell)   'trims off leading spaces 
     End If 
    Next 
    Set rCell = Nothing    'resets the range to zero 
End Sub 

は(他の何かかもしれない?)そうでない場合はを使用して一つにこれら二つの別々のサブ手順を組み合わせることで実行するコードのどの部分を選択するためにスペースまたはカンマをテストすることです。ありがとう、私はあなたのアイデアを見るのを楽しみにしています。

+0

以下の回答のいずれかがあなたの質問に合っていますか?もしそうなら、私はあなたの質問に閉鎖を与えるために受け入れられた答えの1つをマークすることをお勧めします。 – zedfoxus

答えて

1

あなたは正しい行を考えているようです。メソッドが非常に似ているので、リファクタリングは良いアイデアかもしれません。このお試しください:

' 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 

  • を必要とされているフォーマットの種類は、データ
  • が再参加するためのコードを追加し分割するためにコードを追加示すために、列挙定数を追加し、より多くの機能

    • を追加する必要がありますデータ

    また、テストケースを追加して、この機能が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 
    

    このようなテストの利点は、機能が変更された場合、既存のテストを実行して以前の機能が壊れていないことを確認できることです。次に追加された追加のコードがOKかどうかを確認するテストを追加します。セルの範囲にわたり機能を呼び出すために

    、あなたはそれを持っていた方法のように行うことができます。

    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 
    
  • 0

    ここで(任意のミドルネームを含む)の名前

    を反転するいくつかのコードがある
    Sub TestFlipName() 
        Debug.Print FlipName("First Middle Last") 
        Debug.Print FlipName("Last, First") 
    End Sub 
    
    Function FlipName(sName As String) As String 
    
        Dim i As Long 
        Dim NameArray() As String: NameArray = Split(Replace(sName, ",", "")) 
    
        If InStr(sName, ",") Then 
         For i = 1 To UBound(NameArray) 
          FlipName = FlipName + NameArray(i) + " " 
         Next i 
         FlipName = FlipName + NameArray(0) 
        Else 
         FlipName = NameArray(UBound(NameArray)) + ", " 
         For i = 1 To UBound(NameArray) 
          FlipName = FlipName + NameArray(i - 1) + " " 
         Next i 
         FlipName = Trim(FlipName) 
        End If 
    End Function 
    
    関連する問題