• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:【VBA】【正規表現】)

VBAと正規表現を使用して数列から特定の数字を抜き出す方法

このQ&Aのポイント
  • 23歳OLがVBAと正規表現を使用して数列から特定の数字を抜き出す方法を質問しています。
  • 具体的には、数列中の特定の数字のみを抜き出す方法について教えてほしいようです。
  • 質問には実際に書いたコードとその一部の部分を入れるべき箇所が示されています。

質問者が選んだベストアンサー

  • ベストアンサー
  • imogasi
  • ベストアンサー率27% (4737/17068)
回答No.2

#1のご回答と同アイデアですが、 RegExpを使うまでもないと思う。 Sub test01() ’MsgBox Range("A100").End(xlUp).Row lrow = Range("A100").End(xlUp).Row For i = 1 To lrow a = Split(Cells(i, 1), "|") MsgBox a(2) Next End Sub ーーー 区切った文字列の3番めを、繰り返しのコードで探索する必要がないところが違い。 簡略化するために単純な例にしているが、例をもう少し詳しく文章で表現すべきだ。 私の回答が、使えないばあいは、すぐ作れる(あり得る)から。

全文を見る
すると、全ての回答が全文表示されます。

その他の回答 (1)

  • notnot
  • ベストアンサー率47% (4846/10257)
回答No.1

正規表現よりSplit関数を使った方が良いですね。 Sub Sample2() Dim i As Long, msg As String Dim x() As String For i = 1 To 10 x = Split(Cells(i, 1), "|") If UBound(x) >= 2 Then msg = msg & Trim(x(2)) & vbCrLf End If Next i MsgBox msg End Sub

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • 正規表現について

    ExcelにてVBScriptの正規表現を使用していますが、 理解できないところがあるので、教えてください 1.EXCEL VBAにて下記のコードがあります Sub Sample2() Const strCHK As String = "000AAA111BBB2222BBB333" Dim RE, strPattern As String, i As Long, msg As String, reMatch, Item Set RE = CreateObject("VBScript.RegExp") strPattern = "AAA.+bbb" With RE .Pattern = strPattern .IgnoreCase = True .Global = True Set reMatch = .Execute(strCHK) For Each Item In reMatch Debug.Print Item.Value & " FirstIndex→" & Item.FirstIndex & " Length→" & Item.Length Next End With Set reMatch = Nothing Set RE = Nothing End Sub 2.上記を動作させると、イミディエイトに下記が出力されます AAA111BBB2222BBB FirstIndex→3 Length→16 3.疑問 strPattern = "AAA.+bbb"にて検索しているのに、 AAA111BBBが表示されないのは、何故なのでしょうか? よろしくお願いします

  • エクセル マクロ 英語のデータから検索

    どなたかご教示頂ければ幸いです。 以下のようなデータがある場合に、 Japanese team Made in Japan in the Japanese music scene American team Made in USA in the American music scene 次のようなVBAを作りました。 Sub macro1() Dim re, strpattern As String, i As Long, msg As String, rematch Set re = CreateObject("vbscript.regexp") strpattern = "japan.*" With re .Pattern = strpattern .ignorecase = True .Global = True For i = 1 To 10 Set rematch = .Execute(Cells(i, 1)) If rematch.Count > 0 Then msg = msg & rematch(0) & vbCrLf End If Next i End With MsgBox msg Set rematch = Nothing Set re = Nothing End Sub すると、msgボックスには Japanese team Japan Japanese music sceneと表示されます。 私はヒットしたセルの内容を、以下のように全て表示させたいと思っております。 Japanese team Made in Japan in the Japanese musci sceneと・・・ どうすればよろしいでしょうか? 是非とも宜しくお願い申し上げます。

  • 文字列中の両丸括弧を取り除くVBA正規表現

    文字列中の両括弧を取り除く正規表現を求めています. 入力文は「(文字列1)文字列2」となっています. 両括弧は全角の丸括弧"(",")"と半角の丸括弧"(",")"のペアを見つけ,文字列2を取り出したいのです. 文字列1,文字列2は全角,半角の文字列(主に全角)が来ます.括弧の中に括弧が入れ子(ネスト)する事は想定していません. 例えば,「(グループA)田中」という入力に対しては,「田中」をExcel VBAで抽出したいのです. 正規表現ライブラリを使っていますが,別の方法でも効率良く抽出できるならOKです. '---------------  以下を試した結果  --------------------------------- Dim RE, strPattern As String, reMatch Set RE = CreateObject("VBScript.RegExp") ' 'strPattern = "^(\(|().*(\)|))$" ' 'strPattern = "(?!.*[(|\(].+?[\)|)])" ' "(グループA)"にマッチする ' 'strPattern = "([\(.+?\)|(.+?)])" ' 'strPattern = "(^[(|\(].+?[\)|)])" ' strPattern = "^(?!.*[(|\(].+?[\)|)])" ' "" ' 'strPattern = "\(.+?\)" ' strPattern = "^(?!.*[\(.+?\)|(.+?)])" ' "" ' strPattern = "(?!.*[(.+?)])" ' "" ' strPattern = "(.+?)" ' "(グループA)"にマッチする ' strPattern = "^[?!.*((.+?))]" ' ' strPattern = "[^((.+?))]" ' ' '.Pattern = "^(?!.*xyz)" ' 'strPattern = "^(?!.*([.+?]))" ' "" ' strPattern = "[^(.*)]" '"グ","ル",,, ' strPattern = "^(?!([.*]).*)" ' "" ' strPattern = "^(?!(.*).*)" ' "" ' strPattern = "^(?!([.*]))" ' "" strPattern = ").*$" ' "" With RE .Pattern = strPattern .IgnoreCase = True .Global = True Set reMatch = .Execute(str) If reMatch.Count > 0 Then str = reMatch(0).Value End If End With '開放 Set reMatch = Nothing Set RE = Nothing

    • ベストアンサー
    • CSS
  • vbaでの正規表現について教えてください

    「セルに入ってる値が数値ならA列からE列まで赤色にする」 を正規表現で行いたいのですがよくわかりません。 http://officetanaka.net/excel/vba/tips/tips38.htm を参考にしているのですが Sub Sample1() Dim RE, strPattern As String, r As Range Set RE = CreateObject("VBScript.RegExp") strPattern = "SUM\(" With RE .Pattern = strPattern ''検索パターンを設定 .IgnoreCase = True ''大文字と小文字を区別しない .Global = True ''文字列全体を検索 For Each r In ActiveSheet.UsedRange If .test(r.Formula) Then r.Interior.ColorIndex = 3 Next r End With Set RE = Nothing End Sub を、どう改造すれば、私のやりたい事になるのでしょうか? 【1】 まずstrPattern で、「数値ならば」はどうやればいいでしょうか? 【2】 次にtest(r.Formula)は、数式だからFormulaを使ってるのですよね? 数値を検索する場合はFormulaを何に変えればいいでしょうか? 【3】 最後に、 r.Interior.ColorIndex = 3はそのセルだけの色を変えるのですよね? A列からE列までにするにはどうすればいいでしょうか? 例えば A3セルに「1」が、A6セルに「3」が入っていたら、 A列からE列まで赤色にしたいです。

  • Excel : 正規表現を利用して2文字の全角数字を半角数字に変換するには?

    アクティブセルの文字を、正規表現を利用して文字の変換をしたいと考えています。 2文字の全角数字を半角数字に変換します。 かつ、3文字以上の全角数字は変換しません。 下記のようにコードを書いたのですが、希望通りに動作してくれません。 どこが悪いのでしょうか? ご指摘いただければ幸いです。 よろしくお願いいたします。 ※参照可能なライブラリファイルにて、「Microosft VBScript Regular Expressions 5.5」に  チェックは入れています。 Sub sample()   Dim str   Dim strPattern As String   Dim strReplacement As String      str = ActiveCell.Value   str = myRegExp(str, "([^0123456789])([0123456789]{2})([^0123456789])", "$1$2$3")   ActiveCell.Value = str End Sub Private Function myRegExp(str, strPattern, strReplacement)   Dim objRegExp As RegExp   Dim test As String   Set objRegExp = New RegExp   With objRegExp     .Pattern = strPattern     .IgnoreCase = False     .Global = True     myRegExp = .Replace(str, "$1" & StrConv("$2", vbNarrow) & "$3")   End With   Set objRegExp = Nothing End Function

  • vbaで正規表現

    正規表現のコードなんですが、 上手く動きません。 何故でしょうか… Sub Test() Dim reg As Object Dim ans As Object Dim c As Range         Set reg = CreateObject("VBScript.RegExp")     For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))             With reg             .Pattern = "^【[(1)-(20)](\d*/\d*)"             Set ans = .Execute(c.Value)         End With                 If ans > 0 Then             If Len(ans(0).submatches(0)) > 0 Then                             Debug.Print c.Address & "|" & ans(0).submatches(0)                             End If         End If             Next     End Sub

  • VBA初心者なのですが(Userformについて)

    まずは質問ご覧いただきありがとうございますm(_ _)m さっそくなのですが、次のプログラムを打つとSelect Caseのところで”指定されたオブジェクトは見つかりません”と出てしまうのですがなぜでしょうか。回答お待ちしております。 Private Sub CommandButton2_Click() Dim msg As String, i As Integer Dim ii As Integer, msg2 As String For i = 1 To 3 If Controls("CheckBox" & i).Value = True Then msg = msg & Controls("CheckBox" & i).Caption & vbCrLf End If Next i For ii = i To 2 If Controls("OptionBotton" & i).Value = True Then msg2 = msg2 & Controls("OptionBottob" & i).Caption & vbCrLf End If Next ii Select Case Controls("CheckBox" & i).Value & Controls("OptionBotton" & i).Value Case Controls("CheckBox" & i).Value = True & Controls("OptionBotton" & i).Value = False MsgBox msg & "がチェックされてます" Case Controls("CheckBox" & i).Value = False & Controls("OptionBotton" & i).Value = True MsgBox msg2 & "オン" Case Controls("CheckBox" & i).Value = True & Controls("OptionBotton" & i).Value = True MsgBox msg & "がチェックされています" & vbCrLf & msg2 & "オン" Case Else MsgBox "チェック又は、オンにしてください" End Select End Sub

  • VBAの正規表現で特定のフレーズの出現回数を数えるには?

    VBスクリプトでの正規表現について質問します。 例として、次のようなプログラムを作成し、メッセージボックスに"2回"と表示されるような結果を期待しているのですが、"1回"と表示されて困っています。2回と表示されるようにするには、どのようにすればよいのでしょうか?アドバイスをお願いします。 Dim objRegex, objMatches, objMatch As Object Dim intRslt As Integer Dim strSource,strPattern As String Set objRegex = CreateObject("VBScript.RegExp") strSource = "お米の国と呼ばれる日本。お米のお国処は、秋田" strPattern ="お米.*国" With objRegex .Pattern = strPattern .ignorecase = True '.Global = True End With Set objMatches = objRegex.Execute(strSource) intRslt = objMatches.Count msgbox(intRslt & "回") エクセルに使用するため、"VBScript.RegExp"での動作環境になります。

  • これは正規表現とは言えないでしょうか?

    正規表現を勉強しようと思っているのですが まずはじめに確認させて下さい。VBAです。 Sub 正規表現() If a Like "*a*" Then End If End Sub は正規表現とは言えないでしょうか? *を使っているからそう思いました。 Dim re As RegExp Set re = New RegExp を使わないと、正規表現といえないのでしょうか? よろしくお願い致します。

  • ExcelのVBA 正規表現でタブを利用するには?

    Excel2010を利用しています。 正規表現の置換でスペースをタブに変更したいです。 下記で試しましたが、タブへは置換できず「\t」という文字がそのまま表示されてしまいます。 どのようにすれば解決しますでしょうか? Dim reg Dim pat As String Set reg = CreateObject("VBScript.RegExp") With reg .pattern = "(\d+?)[ | ]" .ignoreCase = True .Global = True End With fnsave = "テキスト.txt" numff = FreeFile Open fnsave For Output As #numff Dim i As Integer For i = 2 To 10 temp = Cells(i, "BW") temp = reg.Replace(temp, "$1\t") Print #numff, temp Next Close #numff

このQ&Aのポイント
  • JUNO-DS61でUSBに録音した曲と同時演奏する際、あらかじめ選択した1音色しか使用できないのでしょうか?
  • PATTERN SEQUENCERでUSER登録した8色の音色を演奏することは可能でしょうか?
  • 可能であれば、その方法を教えていただけると嬉しいです。
回答を見る

専門家に質問してみよう