• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:エクセルのマクロで外部データに相対参照)

エクセルのマクロで外部データに相対参照

R_Moonの回答

  • R_Moon
  • ベストアンサー率60% (9/15)
回答No.1

tanaka2030 さん、こんにちわ。 ActiveWorkbook.Path で開いてるブックのパスを取得できますので ブックと同じフォルダに外部データを置けば いけそうですが、これではダメですか?

tanaka2030
質問者

お礼

回答ありがとうございます。 質問に記載したプログラムのどこにActiveWorkbook.Pathを入れたらよいかわかりません。

関連するQ&A

  • このマクロ処理をVBAにしたいのですが…

    テキストファイル→Excelへの取込み処理をVBAで記述したいのですが、上手く行きません。どなたか、ご存知の方いらっしゃいましたらご回答よろしくお願いします。 マクロ処理で試すとこんなカンジなのですが… With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\temp\S5097.txt", _ Destination:=Range("A3")) .Name = "SXXXX" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 932 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileOtherDelimiter = "|" .TextFileColumnDataTypes = Array(1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub

  • Excel VBA 【QueryTables.Add】について

    おしえてください。 マクロの自動記録で、外部からのデータ取り込みをしましたら 以下の様になりました。 With ActiveSheet.QueryTables.Add(Connection:= _ "○○;■■;●●;;□□;▲▲", Destination:=Range("A1")) .CommandText = Array("SELECT * FROM △△.▽▽") .Name = "☆☆ からのクエリ" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .PreserveColumnInfo = True .Refresh BackgroundQuery:=False End With このコードについて勉強したくてヘルプやネットで調べましたが どうしても「.Name …」以降の説明がみつかりません; 詳しく説明の載っているサイトもしくは本などご存じでしたら 教えていただけないでしょうか。 よろしくお願い致します。

  • Excelマクロ 繰り返し?の設定方法を教えて下さ

    BetMasterという競馬ソフトに記述されている 「TXT\2」のデータを抽出し、Sheets("出馬表集計")に貼り付け、 「TXT\1」のデータを抽出し、Sheets("結果集計")に貼り付ける 以下のようなマクロを組んでいます。1日終わるごとにTXT\2とTXT\1を作成してこの集計をしていたのですが、約1年間サボってしまい100回近く、このマクロを作動させなければならなくなりました。 そこで、TXT\1~100まで作成して、TXT\2とTXT\1の貼り付けが終わったら、「TXT\4とTXT\3」、「TXT\6とTXT\5」、「TXT\8とTXT\7」...............と「TXT\100とTXT\99」まで繰り返し抽出と貼り付けを行うようにしたいのですがどうしたらよいでしょうか。 問題は、50回繰り返すことと、2回目以降は前回終了の次の行に貼り付けるという点です。 よろしくお願い致します。 'BetMasterから出馬表データの取り込み ActiveWorkbook.Worksheets.Add With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\BetMaster\TXT\2.", Destination:=Range("A1")) .Name = "1." .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .Refresh BackgroundQuery:=False End With 'Sheets("出馬表集計")に貼り付け Cells.Select Application.CutCopyMode = False Selection.Copy Sheets("出馬表集計").Select Range("A1").Select ActiveSheet.Paste Sheets("Sheet1").Select ActiveWindow.SelectedSheets.Delete 'BetMasterから結果データの取り込み ActiveWorkbook.Worksheets.Add With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\BetMaster\TXT\1.", Destination:=Range("A1")) .Name = "1." .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .Refresh BackgroundQuery:=False End With 'Sheets("結果集計")に貼り付け Cells.Select Application.CutCopyMode = False Selection.Copy Sheets("結果集計").Select   Range("A1").Select ActiveSheet.Paste Sheets("Sheet1").Select ActiveWindow.SelectedSheets.Delete

  • 【ExcelVBA】外部データ取り込みマクロ

    お世話になります。 日付からファイル名を探し出し、外部データの取り込みのマクロで インポートさせたいと考えておりますがファイル名が存在しないと エラーが表示されます。VBAを始めたばかりなのでおかしい場所が解りません。 ご教示いただけますでしょうか。 ---------------------------------------------- ・既存データを削除する。 ・前月度のテキストデータを取得し、インポートを行う。 ・外部データの取り込みはマクロ記録を使用し修正。 ・変数にファイル名が格納された事は確認済み。 ・ファイルパス:固定 ・ファイル名規則:「yymm.txt」 ---------------------------------------------- Sub データ入力表() Worksheets("データ入力表").Activate Columns("A:B").ClearContents imptest Worksheets("セッション数").Activate End Sub Sub imptest() yy = Year(Date) mm = Format(Date - 30, "mm") yymm = Right(yy, 2) & mm & ".txt" With ActiveSheet.QueryTables.Add(Connection:="TEXT;I:\temp\%yymm%" _ , Destination:=Range("A1")) .Name = "1011" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 932 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 9, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub

  • CSVデータをEXCELシートに展開したい

    CSVデータをEXCELシートに展開しようとしています。 シート1にボタンを用意して、クリックするとシート2に展開されるというようなことをしたいのです。 以下のコード(マクロを組んで作られるコードです)だと、シート2に取り込まれないです。 どのようにすればシート2に展開されるでしょうか? Private Sub CommandButton2_Click() Dim SH As Worksheet ' Set SH = Worksheets("Sheet2") With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\temp\test1.", _ Destination:=Range("A1")) .Name = "test1 .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) .Refresh BackgroundQuery:=False End With よろしくお願いします。

  • エクセルVBA URLの記述をセル参照に

    VBA初心者です。WEBクエリの作業をマクロで記録したものを編集しています。 そこで、コードのURL部分をセル参照にしたいのですが、コンパイルエラーが出るため記述が間違っているようです。もともとが間違っているのかどうかもわからず、その点につきまして、ご教授をお願い申し上げます。 Connection:="URL;http://www.○○○○" ↓編集 (URLを入れているセルは、Sheet1のA1、データを返すのはSheet2のA1) Connection:="ActiveWorkbook.WorkSheets("Sheet1").Range("A1")" "Sheet1"部分が選択された状態でエラーとなります。 以下は、ソースです。 Sub test1() With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://www.○○○○", Destination:=Range _ ("$A$1")) .Name = "151" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "22" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub

  • データ読み込み→数行ごとの折り返しについて

    このようなマクロを作りました。 ------------------------------------------------- Sub test() 'TXTファイル読み込みダイアログボックス Dim FileToOpen As String FileToOpen = Application.GetOpenFilename("テキストファイル (*.txt), *.txt") '読み込んだファイルの計算 With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & FileToOpen, Destination:=Range("B11")) ' .Name = "*" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = False .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = xlWindows .TextFileStartRow = 4 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9) .Refresh BackgroundQuery:=False End With End Sub ------------------------------------------------- これだけでは、B列に並んでしまうだけなのでこれを20データごと次の列に入力できるようにしたいのですがいろいろ調べたのですがわかりませんでした。 読み込むテキストは100行あると考えてください わかる方がいましたらよろしくお願いします。

  • Excel VBでのデータの取り込み方

    Excel VBを使って、デスクトップにあるcsv形式のデータを取り込むマクロを作りました(「マクロの記録」にて)。データは10個あるので、同じようなマクロを10個作っています。 そこで、取り込むcsvのファイル名ですが、01.csv、02.csv、…、10.csvという名前になるように1つ1つ名前の変更をしてから取り込んでいます。 これをもとのファイル名のままで、番号の若いもの順から取り込むということは出来ないでしょうか。 元のファイル名は「1002.csv」「1234.csv」「3456.csv」などと「(4ケタの数字).csv」です。 今、使っているVBは以下の通りです(長くてすみません)。 どうぞよろしくお願いいたします。 ------------------------------------------------------------------------ Sub data01取り込み() ' ' data01取り込み Macro ' ' Sheets("data1").Select Range("A1").Select With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\user\Desktop\データ\01.csv", Destination:=Range("$A$1")) .Name = "cell1" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 932 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Range("E2:E400").Select Selection.Copy Sheets("計算").Select ActiveWindow.SmallScroll Down:=-16 Range("e4").Select ActiveSheet.Paste End Sub ----------------------------------------------------------------

  • データのある所まで指定したい

    下記のようにB3からB80までマクロ実行できる様に設定しておきます。 例えばB38までしかデータがない場合、B39でデバッグになってしまいます。 データの入っている所まで実行できるようにお願い致します。 e = 70 ***************************************************** Dim y3 As String y3 = Range("B3").Value Dim cp3 As String cp3 = Range("P3").Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y3, Destination:=Range("E" & e + 1)) .Name = "000" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "" & cp3 .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With e = Range("E65536").End(xlUp).Row ******************************************************      ・      ・      ・ ****************************************************** Dim y80 As String y80 = Range("B80").Value Dim cp80 As String cp80 = Range("P80").Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y80, Destination:=Range("E" & e + 1)) .Name = "000" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "" & cp80 .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With e = Range("E65536").End(xlUp).Row ********************************************************

  • エクセルのマクロ

    エクセルに 1、コマンドボタンを押す 2、貼り付けたいテキストファイルを選ぶ →B3のセルから貼り付ける というマクロを組みました 以下のマクロがそうなんですが、 このマクロだと1つのテキストファイルしか貼り付けれません 2回目以降も貼り付けを同じボタンで繰り返し その前に貼り付けたデータのB列最終セルの一つ下のセルから 同じようにテキスト貼り付けを行えるようにはすることは可能でしょうか? Private Sub CommandButton1_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) Dim vrtSelectedItem As Variant With fd Dim txtpass As Variant If .Show = -1 Then  For Each vrtSelectedItem In .SelectedItems txtpass = "TEXT;" & vrtSelectedItem Next vrtSelectedItem Else End If End With With ActiveSheet.QueryTables.Add(Connection:= _ txtpass, Destination:=Range("$B$3")) .Name = "4" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = False .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 932 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ , 1, 1, 1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub