引数Expressionで指定した文字列の並びを逆にした文字列を作成します。
構文 StrReverse(Expression)
設定項目 内容
Expression 文字の並びを逆にする文字列を指定 [省略不可]
次のサンプルは、この機能を利用した簡単なゲームです。
セルA2からA6までに、人名や地名など、適当な単語を入力した上でマクロを
実行してみてください。
●サンプル●
Sub StrReverseSamp1()
Dim i As Integer
Dim myStr As String
Dim myTime As Single
Range("B2:C6").ClearContents
MsgBox "表示される文字列を逆に読む場合の読み方を入力してください"
myTime = Timer
For i = 2 To 6
myStr = InputBox(Cells(i, 1).Value)
If myStr = "False" Then Exit For
'---(1)入力された文字を逆に並べる
Cells(i, 2).Value = StrReverse(myStr)
If StrComp(Application.GetPhonetic(Cells(i, 1).Value) _
, Cells(i, 2).Value, vbTextCompare) = 0 Then
'---正解の場合
Cells(i, 3).Value = 1
'---不正解の場合
Else
Cells(i, 3).Value = 0
End If
Next
MsgBox "回答にかかった時間 : " & (Timer - myTime) & "秒"
MsgBox "あなたの点数 : " & Application.WorksheetFunction.Sum(Range("C2:C7"))
End Sub
これは、文章を逆に読んだ場合の読み方を答えるゲームです。(1)のステートメント
で、ユーザーが入力した逆方向からの読み方をB列に記入しています。
●ポイント●
StrReverse関数はExcel2000 VBAの新機能です。
|