マクロでデータを区切る方法

このQ&Aのポイント
  • 指定した文字からデータの行位置を検索し、セルを選択する方法を教えてください。
  • 指定した文字の行位置を検索するマクロが必要です。
  • 指定した文字の行からデータを区切る方法を教えてください。
回答を見る
  • ベストアンサー

指定した文字の行からデータ区切り

office2010&Win7Pro(32bit)です。 以下のマクロはtxtデータをExcelで開いて区切る、というマクロです。 <マクロ> Range("A15").Select Range(Selection, Selection.End(xlDown)).Select With Selection.Font .Name = "MS ゴシック" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Selection.TextToColumns Destination:=Range("A15"), DataType:=xlFixedWidth, _ FieldInfo:=Array(Array(0, 2), Array(16, 2), Array(47, 1), Array(73, 1), Array(89, 1), _ Array(113, 1)) Cells.Select With Selection.Font .Name = "MS Pゴシック" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With <説明> 1. セルA15からデータのあるセルまで選択 2. 1.の状態でフォントを「MS ゴシック」にする  ※桁数を揃えるため 3. 1.の状態で区切る 4. 1.の状態でフォントを「MS Pゴシック」に戻す 行位置は都度変更するため、1.と3.の”A15”部分を”指定した文字(例:○○○)”にしたいのです。 1.に関しては「Range("A15").Select」部分を下記マクロに置き換えればできると思いますが、3.の部分をどのようにしたらよろしいでしょうか? Columns("A:A").Select Selection.Find(What:="○○○", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, MatchByte:=False, SearchFormat:=False).Activate ActiveCell.Offset(0, 1).Select ActiveCell.Offset(0, -1).Select ※ActiveCell.Offsetは指定した文字があるセル単体を指定するためにわざわざ入れています

  • KO1014
  • お礼率97% (536/551)

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

  • ベストアンサー
  • mt2015
  • ベストアンサー率49% (258/524)
回答No.1

この手順の↓の2と3は不要だと思いますが? > 1. セルA15からデータのあるセルまで選択 > 2. 1.の状態でフォントを「MS ゴシック」にする  ※桁数を揃えるため > 3. 1.の状態で区切る > 4. 1.の状態でフォントを「MS Pゴシック」に戻す 手順3のDestination:=Range("A15")をどうするかと言う話ですが、この Destinationは省略可能です(省略した場合は左上のセルが使用されます) 以下の様にしてみました。 Columns("A:A").Find(What:="○○○").Select Range(Selection, Selection.End(xlDown)).TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2), Array(16, 2), Array(47, 1), Array(73, 1), Array(89, 1), Array(113, 1))

KO1014
質問者

お礼

ご回答ありがとうございました。 質問のマクロをまるまる掲示していただいたマクロに置き換えたところ、正常に動きました!! 非常に助かりました。

その他の回答 (1)

  • mt2015
  • ベストアンサー率49% (258/524)
回答No.2

すみません、誤記を訂正します 誤:この手順の↓の2と3は不要だと思いますが? 正:この手順の↓の2と4は不要だと思いますが?

KO1014
質問者

お礼

最初、リボンの区切り位置から区切った際に、MS PゴシックのままだとズレまくったのでMS ゴシックに変更していたのです。 掲示していただいたマクロではズレることなく区切れたので、フォント変更マクロは消しました。

関連するQ&A

  • マクロの実行ボタンを削除するマクロ

    以下のようなコードを書いたのですが シート内にボタンができてそのボタンを最終的には削除したいです 資料作成のテンプレートとして下記コードを書いたのですが 資料ができた時にボタンがあるままだと見栄えがいまいちなのでマクロ実行ボタンを削除したくなりました。 シート数は30枚くらいあるので1シートずつマクロ実行ボタンを削除するのは正直しんどいです 一度にシートを全部選択してマクロ実行ボタンを削除したいです マクロ実行ボタンの箇所は全シート同じ箇所にあります Sub ボタン() Selection.Borders(xlEdgeBottom).LineStyle = xlNone Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone ActiveSheet.Buttons.Add(108, 40, 55, 15).Select Selection.OnAction = "図形挿入等倍" Selection.Characters.Text = "図形挿入" With Selection.Characters(Start:=1, Length:=4).Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = 1 End With Range("E4").Select ActiveSheet.Buttons.Add(215, 40, 55, 15).Select Selection.OnAction = "赤枠" Selection.Characters.Text = "赤枠" With Selection.Characters(Start:=1, Length:=2).Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = 1 End With Range("G4").Select ActiveSheet.Buttons.Add(323, 40, 55, 15).Select Selection.OnAction = "テキスト入り赤四角" Selection.Characters.Text = "テキスト" With Selection.Characters(Start:=1, Length:=4).Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = 1 End With Rows("12:12").Select ActiveWindow.FreezePanes = True End Sub

  • Excelマクロ教えてください。

    いつも大変お世話になっております。 あるシートにあるフォントのすべてをMSゴシックにしたいのですが、 マクロ記録→全選択してフォント変更→記録解除 で下記のマクロができました。 Sub Macro1() ' ' Macro1 Macro ' マクロ記録日 : 2007/11/21 ユーザー名 : TEST ' ' Cells.Select With Selection.font .Name = "MS ゴシック" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With End Sub 全選択せずに、あるシートすべてのフォントを変更する方法はありますか? シートのフォントを変更するだけの目的です。 よろしくお願いします。

  • 任意のセルでマクロを実行させたい

    アクティブセルにマクロを実行させたいのですがうまくいきません。 2007のエクセルを使用しています。 (1)命令文で指定しているセル(G9:G11)をJ9:J11やR14:R16等でも使用したい。 (2)また作成したマクロを同シート内オートシェイプに登録したい。 よろしくお願いいたします。 Sub Macro2() ' ' Macro2 Macro ' ' Range("G9:G11").Select Selection.ClearContents With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlTop .Orientation = xlVertical .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With With Selection.Font .Name = "MS P明朝" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 16777164 .TintAndShade = 0 .PatternTintAndShade = 0 End With ActiveCell.FormulaR1C1 = "搬入" With ActiveCell.Characters(Start:=1, Length:=2).Font .Name = "MS P明朝" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With ActiveCell.Characters(1, 2).PhoneticCharacters = "ハンニュウ" Range("G12").Select End Sub

  • Excel マクロ 任意のセルから実行したい

    こんにちは、Excel2003を使用しています。 ExcelでK55からE55までのセルの値を削除して(空白にして) それぞれに「---を引いた透明のダイアローグボックス」を コピーしていくマクロを作成したことがあります。 このときは開始するセルがK55と決まっていたのですが 今度は任意のセルから(たとえば選択したセルの右隣とか) 実行したいのですがどのようにマクロを作ればよいでしょうか ご存じの方お教えください。 なお参考に上記のマクロを記載します。 Range("E55:J55").Select Selection.ClearContents Range("H55").Select ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 672#, 729#, _ 81#, 13.5).Select Selection.Characters.Text = "" With Selection.Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Selection.ShapeRange.Fill.Visible = msoFalse 'Selection.ShapeRange.Fill.Solid 'Selection.ShapeRange.Fill.Transparency = 0# Selection.ShapeRange.Line.Weight = 0.75 Selection.ShapeRange.Line.DashStyle = msoLineSolid Selection.ShapeRange.Line.Style = msoLineSingle Selection.ShapeRange.Line.Transparency = 0# Selection.ShapeRange.Line.Visible = msoFalse ActiveSheet.Shapes("Text Box 12").Select Selection.Characters.Text = "---" With Selection.Characters(Start:=1, Length:=3).Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Selection.HorizontalAlignment = xlCenter Range("K55").Select ActiveSheet.Shapes("Text Box 12").Select Selection.Copy Range("I55").Select ActiveSheet.Paste Range("H55").Select ActiveSheet.Paste Range("G55").Select ActiveSheet.Paste Range("F55").Select ActiveSheet.Paste Range("E55").Select ActiveSheet.Paste Range("E56").Select Selection.Copy Range("F56:J56").Select ActiveSheet.Paste Application.CutCopyMode = False Range("E56:J56").Select Selection.Copy Range("E57:E59").Select ActiveSheet.Paste Application.CutCopyMode = False Range("K59").Select End Sub

  • Excel 任意のセルを指定する方法

    Excel 任意のセルを指定する方法 こんにちは Excel2003でセルの上を「---」で覆うマクロを作成しました。(以下参照) でもこれはセル「K2」に作成されます。 任意の作成したいセルを「---」で覆うようにするのには どのように改造すればよいでしょうか? おわかりの方お教えください。 ' 透明なセルを一つ作るマクロ ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 672.75, 13.5, _ 81#, 13.5).Select Selection.Characters.Text = "---" With Selection.Characters(Start:=1, Length:=3).Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Selection.HorizontalAlignment = xlCenter Selection.ShapeRange.Fill.Visible = msoFalse 'Selection.ShapeRange.Fill.Solid 'Selection.ShapeRange.Fill.Transparency = 0# Selection.ShapeRange.Line.Weight = 0.75 Selection.ShapeRange.Line.DashStyle = msoLineSolid Selection.ShapeRange.Line.Style = msoLineSingle Selection.ShapeRange.Line.Visible = msoFalse Range("K2").Select End Sub

  • マクロ ボタンに名前をつける

    ☆シート1にボタンが29個あり、 ボタンを押すとシート2やシート3のページをアクティブにして表示するマクロを作っています。 その過程で、シート1にボタンを29個作成するところまでは終わりました。 そして、次にボタン1つ1つに名前を付けたいと思うのですが、 シート2、3、4...のI2のテキストを引用してボタンの名前を付けたいです。 (各シートのタイトルはI2に記入しているため。) シート2のI2のテキストを引用してボタンに名前を付けるマクロを作ってみましたが、 ”広告事業収入”がシート2だとは限らないので、”広告事業収入”を引用せず、 ”シート2のI2”を引用してボタンの名前にするマクロにしていただきたいです。 ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・ Sub シート2のボタン作成() ' Sheets(2).Select Range("I2").Select ActiveCell.FormulaR1C1 = "広告事業収入" Sheets(1).Select ActiveSheet.Shapes.Range(Array("Button 1")).Select Selection.Characters.Text = "広告事業収入" With Selection.Characters(Start:=1, Length:=9).Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With End Sub ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・ いらない部分も削除していただけたら幸いです。

  • エクセル 文字色をグレーにしたい

    セルの書式設定→フォント→色で左下のグレーを選択するマクロの記録を取ったのですが そのマクロを実行すると、文字色が白になってしまいます。 コードはこちらです。 Sub Macro() With Columns("a").Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 8 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.499984741 .ThemeFont = xlThemeFontNone End With End Sub マクロの記録の時はグレーになるのに 実際使うと白になるのはなぜでしょうか?

  • Excel VBAで表組みしたらデバック発生

    Excel VBAの初心者です。Windows Vistaで Excel2007を使っています。 表をマクロの実行で作成したいと思っています。 何もないエクセルブックより 「開発」→「マクロの記録」→「相対参照」 →「表の作成」→「記録終了」→「相対参照で記録の解除」 →「エクセルマクロ有効ブックで保存」 ところがこのマクロ記録が入ったブックを再度立ち上げ、 表をオールクリアにし、マクロボタンより表作成を実行 させようとすると、次のエラーメッセージがでました。 『実行時エラー'9' インデックスが有効範囲にありません。』 デバックからModule1をみると以下の記述となっていました。 Sub 表組み() ' ' 表組み Macro ' ' ActiveCell.Range("A1:E5").Select Selection.Copy Windows("Book1").Activate ActiveSheet.Paste ActiveCell.Columns("A:A").EntireColumn.Select ActiveCell.Rows("1:1").EntireRow.RowHeight = 11.25 ActiveCell.Rows("1:5").EntireRow.Select Selection.RowHeight = 21.75 ActiveCell.Columns("A:A").EntireColumn.ColumnWidth = 14.88 ActiveCell.Offset(0, 4).Range("A1").Select Application.CutCopyMode = False With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With ActiveCell.Offset(1, -3).Range("A1:D4").Select Selection.NumberFormatLocal = "#,##0_ " ActiveCell.Select ActiveCell.FormulaR1C1 = "78000" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "102000" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "9800" ActiveCell.Offset(-2, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "65000" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "204000" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "500" ActiveCell.Offset(-2, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "86000" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "151000" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "10200" ActiveCell.Offset(-2, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(RC[-3]:RC[-1])" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(RC[-3]:RC[-1])" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)" ActiveCell.Offset(0, -3).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)" ActiveCell.Offset(0, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)" ActiveCell.Offset(0, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)" ActiveCell.Offset(-4, -2).Range("A1:D1").Select Selection.AutoFilter End Sub 上から9行目(?)のWindows("Book1").Activateに 黄色い矢印が示され、また行全体が黄色く四角に 覆われていました。 おそらくこの記述に問題があると思いますが、 どんな記述に変えたらいいのか分かりません。 Excel VBAにお詳しい方ご教示願います。 なお、マクロで作成したい図を添付いたします。 参考にしていただければ幸いです。

  • EXCELのマクロについて

    お世話になっております。 以下のマクロを1万行分繰り返したいのですが、回数を1万回と指定する構文を 教えてください。よろしくお願いします。 Sub Macro16() ' ' Macro16 Macro ' ' Keyboard Shortcut: Ctrl+Shift+Z ' ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove ActiveCell.Offset(-1, 0).Range("A1:M1").Select Selection.Copy ActiveCell.Offset(1, 0).Range("A1").Select ActiveSheet.Paste ActiveCell.Offset(-1, 2).Range("A1").Select Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "7/5/1905" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "7/6/1905" ActiveCell.Offset(1, -2).Range("A1").Select End Sub

  • マクロ:セルの範囲指定

    エクセルマクロで困っています。 セルの範囲指定をしようとしています。 初心者過ぎて、よくわかりません。 現在のマクロ↓ Sub 済() If ActiveCell.Column = 21 Then Selection.FormatConditions.Delete '条件付き書式削除 With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With '色変え判定セル書き換え ActiveCell.Offset(0, 5).Select ActiveCell.FormulaR1C1 = "77" ActiveCell.Offset(0, -5).Select Else answer = MsgBox("U列を選択して下さい", vbCritical) End If End Sub やりたい事は、下記の通りです。 列Uがアクティブの時にU~ACの行を塗りつぶし。 列は変動します。 今は、やり方がよく分からなかったため オフセットで一つ一つ塗りつぶしてます。 マクロを組みすぎてファイルが重くなって困っています。 回答よろしくお願いいたします。

専門家に質問してみよう