アメリカの大学生活---日記

ミネソタ州立大学ムーアヘッド校 会計学部を5月に卒業して帰国し、税務の仕事してます。

練習問題13(文字列関数の練習) メモ

 

excel-ubara.com

 

Sub Practice13()
    Dim i As Long
    Dim strPref As String
    Dim strCity As String
    Range("G2:I8").ClearContents
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
       If InStr(Cells(i, 1), "(") > 0 Then
           strPref = Left(Cells(i, 1), InStr(Cells(i, 1), "(") - 1)
      Else
           strPref = Cells(i, 1)
      End If
      If InStr(Cells(i, 2), "(") > 0 Then
          strCity = Left(Cells(i, 2), InStr(Cells(i, 2), "(") - 1)
      Else
          strCity = Cells(i, 2)
      End If
      If Left(strPref, Len(strPref) - 1) = strCity Then
          Cells(i, 3) = strPref
      Else
          Cells(i, 3) = strPref & "(" & strCity & ")"
     End If
  Next
End Sub

 

 

メモ:

型:
整数を入れる変数 → 長整数型(Long)
文字列を入れる変数 → 文字列型(String)