目次
関数化 VBAにて右からn文字目の抽出
Function ExtractThirdCharFromRight(inputString As String) As String
If Len(inputString) >= 3 Then
ExtractThirdCharFromRight = Mid(inputString, Len(inputString) – 2, 1)
Else
ExtractThirdCharFromRight = “”
End If
End Function
関数にしない隣セルに出力
Sub ExtractThirdCharFromRightInSelectedCells()
Dim targetCell As Range
Dim extractedChar As String
For Each targetCell In Selection
If Len(targetCell.Value) >= 3 Then
extractedChar = Mid(targetCell.Value, Len(targetCell.Value) – 2, 1)
targetCell.Offset(0, 1).Value = extractedChar
Else
targetCell.Offset(0, 1).Value = “”
End If
Next targetCell
End Sub
以下だと、抽出結果のみに置換
Sub ExtractThirdCharFromRightInSelectedCells()
Dim targetCell As Range
For Each targetCell In Selection
If Len(targetCell.Value) >= 3 Then
targetCell.Value = Mid(targetCell.Value, Len(targetCell.Value) – 2, 1)
Else
targetCell.Value = “”
End If
Next targetCell
End Sub