• ベストアンサー

VBA テキストclose

初めまして。VBAでマクロを作成しています。 今、sheet上のあるボタンを押したら、テキストファイルのデータをカンマ刻みで読み出し、シートに表示しています。 記憶マクロで作成したのですが、以下のコードはtextファイルをopenにしたままなので、closeしたいのです。(外部からテキストファイルに書き込みたいため、openのままであると書き込みができない)。 以下のコードを修正してテキストファイルをクローズさせる方法を 教えていただけないでしょうか?  宜しくお願い致します。 Sub ボタン1_Click() ' ' ボタン1_Click Macro ' マクロ記録日 : 2008/8/10 ユーザー名 : Matsumura ' ' ChDir "C:\Documents and Settings\Owner\デスクトップ\List" Workbooks.OpenText Filename:= _ "C:\Documents and Settings\Owner\デスクトップ\List\DBへ登録.txt", Origin:=932, _ StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True _ , Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _ Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), TrailingMinusNumbers:=True Columns("A:A").ColumnWidth = 14.38 Application.CommandBars("Forms").Visible = False Columns("B:B").ColumnWidth = 14.38 Columns("C:C").ColumnWidth = 14.38 Columns("D:D").ColumnWidth = 14.38 Columns("E:E").ColumnWidth = 14.5 Range("E1").Select Columns("E:E").ColumnWidth = 14.38 Columns("F:F").ColumnWidth = 14.38 Columns("E:E").ColumnWidth = 16.25 Columns("A:F").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Range("B3").Select Rows("1:1").RowHeight = 26.25 Range("A1:F1").Select With Selection.Font .Name = "MS Pゴシック" .Size = 18 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Selection.Font.ColorIndex = 3 Selection.AutoFilter End Sub

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

  • ベストアンサー
  • hige_082
  • ベストアンサー率50% (379/747)
回答No.2

これかな Windows("DBへ登録.txt").Close 試してみて

その他の回答 (1)

  • bin-chan
  • ベストアンサー率33% (1403/4213)
回答No.1

最後のEnd Subの直前に Workbooks.Close を入れれば良いのでは?

関連するQ&A

  • 複数ファイルへのVBAの処理について

    最近、ExcelのVBAを使うようになりました。 しかし、以下のような処理を同じフォルダ内の複数のファイルに対して一気にやりたいのですが、わかる方いらっしゃいませんか? 理想としては同じフォルダ内で50個くらいを選択して一気にやりたいのですが・・・。 ******************************* Sub FFT() Columns("A:A").Select Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _ FieldInfo:=Array(Array(0, 1), Array(18, 1)), TrailingMinusNumbers:=True Range("E8").Select Application.Run "ATPVBAEN.XLA!Fourier", ActiveSheet.Range("$B$1:$B$256"), _ ActiveSheet.Range("$C$1:$C$256"), False, False Columns("C:C").ColumnWidth = 38.38 Range("D1").Select Columns("D:D").ColumnWidth = 20.75 Range("D1").Select ActiveCell.FormulaR1C1 = "=IMABS(RC[-1])" Range("D1").Select Selection.Copy Range("D1:D256").Select ActiveSheet.Paste Range("E6").Select Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "" Range("E7").Select End Sub

  • セル結合と列挿入

    マクロの記録を使い書いて見ましたが、1行置きに3行挿入し 、A2:A5 , B2:B5 ,C2:C5 ,D2,D5 言う感じでセル結合を5000行まで行い、最後にD、E列、列の挿入したいのですが、どのように書けば宜しいでしょうか? すでに、データが入っています。 Sub Macro1() Rows("3:5").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Rows("7:9").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("A2:A5").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("B2:B5").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("C2:C5").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("A6:A9").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("B6:B9").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("C6:C9").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Columns("D:E").Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Selection.ColumnWidth = 18.88 Range("A2:A5").Select End Sub

  • 【自作マクロ】いらない部分を削除していただきたい。

    自分で行ってみたマクロですが、長く、見づらいです。 いらない部分を削除していただける方がいましたら、お願いいたします。 作業としては、 /////////////////////////////////// あああ いいい ううう えええ おおお かかか      あかさたなはまやらわん の、「あかさたなはまやらわん」を削除し、セルの結合を解除し、 あああ、いいい など文字のあるセルと下のセルを結合して格子をつける。 /////////////////////////////////// Sub 項目を1行にして摘要を削除する() ' ' 項目を1行にして摘要を削除する Macro ' ' Range("C7:H7").Select ActiveCell.FormulaR1C1 = "" Range("C7:H7").Select With Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With Selection.UnMerge Range("C6:C7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("D6:D7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("E6:E7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("F6:F7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("G6:G7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("H6:H7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("C6:H7").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With End Sub

  • エクセルVBAで教えて下さい。

    A1のセルに [ 1- 5] 4.05398e-01 3.63385e-01 -2.22992e-01 9.89158e-03 -6.43695e-02 A2のセルに [ 6-10] -5.12224e-04 4.07480e-04 -2.73746e-04 -1.77853e-02 -2.13805e-03 A3のセルに [11-15] -6.88489e-03 -2.06765e-02 -9.44633e-03 6.97059e-03 -1.28400e-02 と、このような感じでA7セルまで同じ感じでスペースで空いた数値が入力されています。 A8のセルのみ [36-37] -6.39210e-03 -1.55806e-03 と入力されております。 まず行いたいのはスペースが空いてる部分で、それぞれの数値を各セルに分けたいです。 A1のセルに入力されている [ 1- 5] 4.05398e-01 3.63385e-01 -2.22992e-01 9.89158e-03 -6.43695e-02 ならば A1に[1-5] B1セルに4.05398e-01 C1セルに3.63385e-01 のように これをA1からA8のセルで行ったあと指定のセルを30行目に貼り付けます。 E1→A29 C2→B29 D2→C29 E2→D29 E3→E29 F3→F29 B4→G29 D5→H29 E5→I29 F5→J29 貼り付けのデータは増えていきます。つまり、30行目にデータが入ってる場合は そのデータが1行下の行に下がり、新たなデータが30行目に追加されます。 このようにして、データが最大で58行目まで追加される可能性があります。 最小であれば30行目、31行目の2つしかない場合あります。 この時、0の近似値を各列のセルから探し、当てはまるセルを赤く塗り潰すというのが 今回行いたいことです。 A列ならA30~A58までの中で0の近似値を探し、当てはまるセルを赤く塗り潰す。 ただ空白の場合は無視してもらいたいです。0の近似値だと空白が選択されてしまうので。 近似値探しの前までならマクロがありますのでご参照下さい。 Sub Macro4() ' ' Macro4 Macro ' ' Range("A1:A8").Select Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _ FieldInfo:=Array(Array(0, 1), Array(7, 1), Array(21, 1), Array(34, 1), Array(47, 1), _ Array(60, 1)), TrailingMinusNumbers:=True Range("A1").Select Range("E1").Select Selection.Copy Range("A29").Select ActiveSheet.Paste Range("C2:E2").Select Application.CutCopyMode = False Selection.Copy Range("B29").Select ActiveSheet.Paste Range("E3:F3").Select Application.CutCopyMode = False Selection.Copy Range("E29").Select ActiveSheet.Paste Range("B4").Select Application.CutCopyMode = False Selection.Copy Range("G29").Select ActiveSheet.Paste Range("D5:F5").Select Application.CutCopyMode = False Selection.Copy Range("H29").Select ActiveSheet.Paste Range("J7").Select Application.CutCopyMode = False Range("A29:K29").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("A29:K29").Select With Selection.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With Range("A29").Select Range("A1:F8").Select Selection.ClearContents Range("A1").Select Range("K29").Select With Selection.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With Selection.NumberFormatLocal = "G/標準" End Sub わかりずらい質問ですみませんが、ご指導の程 お願い致します。

  • excelのマクロが上手く動作しません

    excel2013で、シートのレイアウトを整えるマクロを以下のように作成しました。 しかし、いざ他のシートで試すと、そのシートと同時に特定のシートにもなぜかマクロが実行されてしまいます。(恐らく作ったときに使っていたシート) どうしたら今見ているシートだけにマクロを実行することができるでしょうか? ActiveWindow.Zoom = 85 ActiveWindow.Zoom = 70 Columns("A:A").ColumnWidth = 10.13 Columns("A:A").ColumnWidth = 10.63 Columns("B:B").ColumnWidth = 6.63 Columns("D:D").ColumnWidth = 20 Columns("D:D").ColumnWidth = 23.75 Columns("D:D").ColumnWidth = 24.63 Columns("E:E").ColumnWidth = 10.38 Range("E1").Select ActiveCell.FormulaR1C1 = "インボイス金額" ActiveCell.Characters(7, 2).PhoneticCharacters = "キンガク" Columns("F:F").Select Selection.Delete Shift:=xlToLeft Columns("G:G").Select Selection.Delete Shift:=xlToLeft Range("H9").Select Columns("G:G").ColumnWidth = 11.38 Columns("I:K").Select Selection.Delete Shift:=xlToLeft Range("J9").Select Columns("I:I").ColumnWidth = 15 Columns("J:J").ColumnWidth = 9.75 Columns("K:K").ColumnWidth = 9.5 Range("L2").Select Columns("L:L").ColumnWidth = 5.5 Columns("O:O").Select Selection.Delete Shift:=xlToLeft Selection.ColumnWidth = 13.88 Columns("P:P").Select Selection.Delete Shift:=xlToLeft Selection.ColumnWidth = 12.13 Selection.ColumnWidth = 13.25 ActiveWindow.ScrollColumn = 2 ActiveWindow.ScrollColumn = 3 ActiveWindow.ScrollColumn = 4 ActiveWindow.ScrollColumn = 5 ActiveWindow.ScrollColumn = 6 ActiveWindow.ScrollColumn = 7 ActiveWindow.ScrollColumn = 8 ActiveWindow.ScrollColumn = 9 ActiveWindow.ScrollColumn = 10 ActiveWindow.ScrollColumn = 11 ActiveWindow.ScrollColumn = 12 Columns("Q:Q").Select Selection.ColumnWidth = 5.5 Columns("S:S").Select Selection.ColumnWidth = 6.75 Columns("T:T").ColumnWidth = 9.75 Columns("Q:T").Select Range("T1").Activate Selection.Style = "Comma [0]" Columns("U:U").Select Columns("V:V").ColumnWidth = 5.5 Columns("V:V").ColumnWidth = 6 Columns("W:Z").Select Selection.Delete Shift:=xlToLeft Columns("X:Z").Select Selection.Delete Shift:=xlToLeft Range("X10").Select Columns("X:X").ColumnWidth = 12.25 Columns("X:X").ColumnWidth = 11.13 Columns("Y:Y").ColumnWidth = 6.75 Columns("Z:Z").ColumnWidth = 11.63 Columns("AA:AA").ColumnWidth = 6.75 Columns("AB:AB").Select Selection.Delete Shift:=xlToLeft Range("AB2").Select Columns("AB:AB").ColumnWidth = 11 Columns("AD:AO").Select Selection.Delete Shift:=xlToLeft Range("AF14").Select Columns("AD:AD").ColumnWidth = 11 Columns("AE:AE").Select Selection.Delete Shift:=xlToLeft Range("AG7").Select Columns("AE:AE").ColumnWidth = 20.38 Columns("AF:AJ").Select Selection.Delete Shift:=xlToLeft ActiveWindow.ScrollColumn = 21 ActiveWindow.ScrollColumn = 1 Rows("2:2").Select ActiveWindow.FreezePanes = True Range("A2").Select ActiveWorkbook.Worksheets("11").Sort.SortFields.Clear ActiveWorkbook.Worksheets("11").Sort.SortFields.Add Key:=Range("D2:D137"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets("11").Sort.SortFields.Add Key:=Range("A2:A137"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("11").Sort .SetRange Range("A1:AE137") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .Apply End With End Sub よろしくお願い致します。

  • エクセルマクロで教えてください

    エクセル2003です。 自動マクロで下記のようなマクロを造ったんですが Selection.End(xlDown).Select   Range("A29:D29").Select  ■A29を止まったセルの番号にしたいのです。(A**からD**まで)     With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("A30").Select ■A30を止まったセルの番号にしたいのです 以上2箇所の指定を教えていただけますでしょうか。 よろしくお願いいたします。

  • excel vba

    (1)下記のマクロに出てくるApplication.CutCopyMode = False というのは何でしょうか。 (2)また自分でマクロ記録してあるシートを、別のシートにコピーしたとき Application.CutCopyMode = False Selection.Copy というのが、付くときとつかないときがあったんですがなぜそのようなことがおきるのか。 (3)またSelection.Copyというのは何ですか。 Sub Macro1() ' ' Macro1 Macro ' マクロ記録日 : 2007/1/11 ユーザー名 : ××××× ' ' Workbooks.Open Filename:="C:\aaa\bbbbb\cccc\aaaa_1.xls" Columns("H:H").Select Application.CutCopyMode = False Selection.Cut Columns("E:E").Select Selection.Insert Shift:=xlToRight Columns("I:I").Select Selection.Cut Columns("G:G").Select Selection.Insert Shift:=xlToRight With Selection.Font .Name = "MS 明朝" .Size = 10 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With ActiveSheet.PageSetup.PrintArea = "$A$1:$L$61" ActiveWorkbook.Save End Sub

  • VBAマクロエラー【オーバーフローしました。】

    以下のVBAマクロで突然エラーが出るようになってしまいました。 原因がわからず困っています。 どなたかご教授ください。 該当部分 :S = Range("B2").End(xlDown).Row エラーMrg:実行時エラー'6': オーバーフローしました ----------マクロ文---------- Sub 部担コード読み替え() Dim R_Count As Integer Dim P_Sheet As String Dim S As Integer 'データ取込用のファイルを開く Workbooks(D_Book).Activate Sheets("Data1").Select Columns("C:C").Select Selection.Insert Shift:=xlToRight Range("B2").Select S = Range("B2").End(xlDown).Row Range("C2:C" & S).Formula = "=SUBSTITUTE(SUBSTITUTE(RC[-1],""%"",""1""),""*"",""2"")" Range("C1").Value = "部担コード" Cells.Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues Columns("B:B").Select Application.CutCopyMode = False Selection.Delete Shift:=xlToLeft Range("A1").Select Sheets("Data2").Select Columns("C:C").Select Selection.Insert Shift:=xlToRight Range("B2").Select S = Range("B2").End(xlDown).Row Range("C2:C" & S).Formula = "=SUBSTITUTE(SUBSTITUTE(RC[-1],""%"",""1""),""*"",""2"")" Range("C1").Value = "部担コード" Columns("G:G").Select Selection.Insert Shift:=xlToRight Range("G2:G" & S).Formula = "=SUBSTITUTE(SUBSTITUTE(RC[-1],""%"",""1""),""*"",""2"")" Range("G1").Value = "キー" Cells.Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues Columns("B:B").Select Application.CutCopyMode = False Selection.Delete Shift:=xlToLeft Columns("E:E").Select Application.CutCopyMode = False Selection.Delete Shift:=xlToLeft Range("A1").Select End Sub

  • エクセルVBAで列幅設定

    A列の幅を、C~F列の幅(同一ではありません)に設定しようと思いました。 ところが Columns("A").ColumnWidth = Range("C1:F1").Width とすると、ColumnWidthとWidthの単位がまったく違うのでエラーになります。 同じ単位で設定するには Columns("A").ColumnWidth = Columns("C").ColumnWidth + Columns("D").ColumnWidth + Columns("E").ColumnWidth + Columns("F").ColumnWidth とするか、 Sub test02() Dim c As Range Dim x As Single For Each c In Range("C1:F1") x = x + c.ColumnWidth Next Columns("A").ColumnWidth = x End Sub などのように手の込んだことをするしか思いつきません。 もっと簡単な方法はないでしょうか?

  • Excelにて、列の幅をマクロで変えるには?

    今、提出用の資料作成にて、 従業員の稼動実績を記載した表部分を隠して A3にかたち良く収まるように印刷できるよう列の調整をしようと マクロの記録を行い、下記のようなプログラムを得ました。 ところが、実行すると("U:AD")の部分は隠れているのですが、 それ以外は全て、列の幅が"20"になってしまいます。 一体どのようにすれば列の幅を記載通りに調整できるのでしょうか? お教え下さい。宜しくお願いします。 * * * * * * * * * * * * * * * * * Sub 稼動実績を隠す() ' ' 稼動実績を隠す Macro ' マクロ記録日 : 2007/9/27 ユーザー名 : ######## ' ' Columns("A:M").Select Range("A2").Activate Selection.ColumnWidth = 8 Columns("N:O").Select Selection.ColumnWidth = 16 Columns("P:P").Select Selection.ColumnWidth = 20 Columns("R:T").Select Selection.ColumnWidth = 20 Columns("U:AD").Select Selection.ColumnWidth = 0 Columns("AE:AF").Select Selection.ColumnWidth = 20 Columns("AG:AG").Select Selection.ColumnWidth = 8 Columns("AH:AH").Select Selection.ColumnWidth = 54 Columns("AI:AP").Select Selection.ColumnWidth = 20 Columns("AQ:AQ").Select Selection.ColumnWidth = 8 Columns("AR:AS").Select Selection.ColumnWidth = 20 Range("A2").Select End Sub

専門家に質問してみよう