アプリ版:「スタンプのみでお礼する」機能のリリースについて

画像のように、A列の文字をC列のようにしたいと考えています。
http://oshiete.goo.ne.jp/qa/4307830.htmlを参考に以下のコードとしました。

Sub test01()

Dim i As Integer, n As Integer, s As Integer, l As Integer

i = Len(ActiveCell)

For n = 1 To i
If ActiveCell.Characters(n, 1).Font.Underline <> xlNone Then
s = n
Exit For
End If
Next

If s > 0 Then
For n = s + 1 To i
If ActiveCell.Characters(n, 1).Font.Underline = xlNone Then
l = n - 1
Exit For
End If
Next
End If

If l = 0 Then
l = i
End If

MsgBox "開始点:" & s & vbCrLf & "終了点:" & l

End Sub

ところが、A5,A6の処理がうまくいきません。また、"["や"]"の挿入の方法もわかりません。
教えていただければ幸いです。よろしくお願いいたします。

使用OS:Windows7
使用ソフト:Microsoft Excel 2000~2010

「ExcelのVBAで下線部分の取得・文字」の質問画像

A 回答 (1件)

じゃあ,これを見て何をやっているのか考えてください。


Sub test01()
Dim i As Long, n As Long
Dim u As Boolean
Dim ss As String
n = Len(ActiveCell)
u = False
For i = 1 To n
If u = False And ActiveCell.Characters(i, 1).Font.Underline <> xlUnderlineStyleNone Then
ss = ss + "["
u = True
ElseIf u = True And ActiveCell.Characters(i, 1).Font.Underline = xlUnderlineStyleNone Then
ss = ss + "]"
u = False
End If
ss = ss + ActiveCell.Characters(i, 1).Text
Next i
If u = True Then
ss = ss + "]"
End If
ActiveCell.Offset(, 1) = ss
End Sub
    • good
    • 1
この回答へのお礼

早速の回答ありがとうございました。望む通りの結果となりました。
ssの変数の使い方、true,falseによる分岐、勉強になりました。
このたびはありがとうございました!

お礼日時:2012/08/07 19:15

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!