指定範囲までデータを取得するマクロ | Excel VBA

このQ&Aのポイント
  • データのある所まで指定範囲までマクロを実行できる設定方法について解説します。
  • 指定した範囲までデータを取得するために、実行するマクロの設定方法について説明します。
  • データが入っている範囲まで実行できるマクロの設定手順を紹介します。
回答を見る
  • ベストアンサー

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

下記のように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 ********************************************************

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

  • ベストアンサー
  • kmetu
  • ベストアンサー率41% (562/1346)
回答No.3

Dim y(80) As String Dim cp(80) As String Dim i As Integer e = 70 For i = 3 To Range("B65536").End(xlUp).Row y(i) = Range("B" & i).Value cp(i) = Range("P" & i).Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y(i), 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 = "" & cp(i) .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With e = Range("E65536").End(xlUp).Row Next i とか…

maki6006
質問者

お礼

再度、回答有難う御座います。 そのままマクロ使用させて頂きます。 追加関連質問ありますが、こちらの質問は一旦閉じさせて頂きます。 新しい質問も回答頂けますと嬉しいです。

その他の回答 (3)

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

データ最終行を捉える方法は (1)Currentregion (2)UsedRange (3)End+↑の操作該当 Sub test02() MsgBox Range("B65536").End(xlUp).Row End Sub (4)編集ージャンプーセル選択ー最後のセルの行 Sub test01() MsgBox Range("a1:C100").SpecialCells(xlCellTypeLastCell).Row End Sub みなくせがある。特徴を捉えて使わないと危ない。 (3)が一番良さそうだが。 ーーー >本題マクロとの組合せを書いて頂けますと助かります。 といっているようでは、こんな回答は無駄かな。

maki6006
質問者

お礼

回答ありがとうございます。 回答のような勉強レベルに達しておりません。 参考までとさせて下さい。

回答No.2

マクロは、 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)) ・・・ End With e = Range("E65536").End(xlUp).Row のセットが、B3からB80までの78個あるのですか?お示しのコードでは、たくさん宣言された変数が泣いています。変数は少数精鋭にしてもっと活用してください。たとえば、 Dim b_val As String 'B列のセルの値 Dim p_val As String 'P列のセルの値 Dim e_lastrow As Integer 'E列のデータのある最終行番号 Dim b_rowno As Integer 'B列の処理中の行番号 For b_rowno = 3 To 80 b_val = Range("B" & b_rowno).Value 'b_rowno行に「http・・・」というデータがあるときだけクエリを実行。 If InStr(b_val,"http") > 0 Then e_lastrow = Range("E65536").End(xlUp).Row With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y_val, Destination:=Range("E" & (e_lastrow + 1))) ・・・ .WebTables = "" & p_val ・・・ End With End If Next という感じです。 ●変更した変数 「y3」などー>「b_val」'B列のセルの値 「cp3」などー>「p_val」'P列のセルの値 「e」ー>「e_lastrow」'E列のデータのある最終行番号 >例えばB38までしかデータがない場合、B39でデバッグになってしまいます。 これに対応するために、B列の値に「http・・・」という文字がある場合だけクエリを実行するように、以下のIf文を追加しました。 If InStr(y_val,"http") > 0 Then ・・・ End If ●For文については、WEBや書籍などを参考にしてください。 For文を覚えるとプログラムがすっきりして見通しが良くなる(バグが入り込む可能性が減る)ので、ぜひ身に付けてください。

maki6006
質問者

補足

回答ありがとうございます。 e = 70 Dim b_val As String 'B列のセルの値 Dim p_val As String 'P列のセルの値 Dim e_lastrow As Integer 'E列のデータのある最終行番号 Dim b_rowno As Integer 'B列の処理中の行番号 For b_rowno = 3 To 80 b_val = Range("B" & b_rowno).Value 'b_rowno行に「http・・・」というデータがあるときだけクエリを実行。 If InStr(b_val,"http") > 0 Then e_lastrow = Range("E65536").End(xlUp).Row With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y_val, Destination:=Range("E" & (e_lastrow + 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 = "" & p_val .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End If Next の "URL;" & y_val, Destination:=Range("E" & (e_lastrow + 1)))       ↓ "URL;" & b_val, Destination:=Range("E" & (e_lastrow + 1))) として実行しましたが何も表示されませんでした。 再度、見て頂けますと助かります。

  • kmetu
  • ベストアンサー率41% (562/1346)
回答No.1

各変数を配列にして For i=3 to Range("B65536").End(xlUp).Row y(i) = Range("B" & i).Value cp(i) = Range("P" & i).Value 中略 e = Range("E65536").End(xlUp).Row Next i にすればいかがでしょう。

maki6006
質問者

お礼

回答ありがとうございます。 素人ですので、本題マクロとの組合せを書いて頂けますと助かります。

関連するQ&A

  • データのある所まで指定したい(2)  文字検索の追加

    E列に結果を表示させますが、それぞれの結果のEセル(左上)に「問題」という文字があればそのまま実行し、 なければ元アドレスであるB列でMsgBox(k & ”行目は間違っています”)と表示させたい。 お願い致します。 Dim y(80) As String Dim cp(80) As String Dim i As Integer e = 70 For i = 3 To Range("B65536").End(xlUp).Row y(i) = Range("B" & i).Value cp(i) = Range("P" & i).Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & y(i), 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 = "" & cp(i) .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With e = Range("E65536").End(xlUp).Row Next i

  • VBAの追加

    同じ様な場所からデータを取り出す時、欲しい情報はWebTables="5"でK3に「5」を入力しておきますが、たまに1つか2つくらい場所違いにより欲しい情報はWebTables="4"の時が存在します。 l3(エル3)に「4」を入力しておいて、WebTables =5に欲しくない情報が入っている場合はWebTables =4を使用するという命令を追加したいと思います。 下記右側のようにセルに「メーカー」の文字が入っていればそのままWebTables =5を使い、「メーカー」の文字が入っていなければWebTables =4を実行 と考えております。 また、それ以外の方法でも構いませんのでお願い致します。 Dim i As Long Dim myAddress As String lastrow = ActiveSheet.Range("A3").End(xlDown).Row d = 50 For i = 3 To lastrow myAddress = Range("B" & i).Value Dim bbb As String bbb = Range("K3").Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & myAddress, Destination:=Range("B" & d + 1)) .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 = "" & bbb .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With d = Range("B65536").End(xlUp).Row Next i                     WebTables =5の場合                     正しく欲しい情報                     111111111111111111         メーカー1111111111                     111111111111111111     →   11111111111111111                     111111111111111111         11111111111111111 WebTables =4の場合 正しく欲しい情報      欲しくない情報が入っている 111111111111111111     222222222222222222 111111111111111111     222222222222222222 111111111111111111     222222222222222222

  • マクロのなかの一部(日付)を書き換えたい

    マクロのなかの一部(日付)を書き換えたくてその部分を変数で 変更したいのですがうまく行きません少し長くなりますが下記の どの部分を直したら良いのでしょうか御教願えませんでしょうか セル Range("K1") に 20081007 の日付が入っています Sub Tuika1() Dim xxx As String xxx = Range("K1") Range("A6").Select With Selection.QueryTable .Connection = "URL;http://www.?????.com/data/daily.cgi/& xxx &.html" .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "5,6,7,10,11,12,13,14,15,16,17,18,19,20" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub

  • 実行時エラー'1004': アプリケーション定義またはオブジェクト定義

    実行時エラー'1004': アプリケーション定義またはオブジェクト定義について Dim code As String Dim lastrow As Integer Dim i As Integer Sub calc() Dim code As String Dim day_s As Integer, month_s As Integer, year_s As Integer Dim day_e As Integer, month_e As Integer, year_e As Integer Dim row_length As Integer code = "998407.o" day_e = 31 month_e = 12 year_e = 2005 day_s = 1 month_ = 1 year_s = 2005 Range("B4:H65536").ClearContents For i = 0 To 365 * 0.65 Step 50 URL = "URL;http://table.yahoo.co.jp/t?s=" & code & "&a=" & month_s & "&b=" & day_s & "&c=" & year_s & "&d=" & month_e & "&e=" & day_e & "&f=" & year_e & "&g=d&q=t&y=" & i & "&z=" & code & "&x=.csv" If i = 0 Then lastrow = 4 Call GETデータ If Range("B4") = "" Then Exit Sub End If Else lastrow = Range("B4").End(xlDown).Row + 1 Call GETデータ Range("B" & lastrow, "H" & lastrow).Delete row_length = Range("B4").End(xlDown).Row If row_length - lastrow < 49 Then Exit For End If End If Next Range("B5:H65536").Sort key1:=Columns("B") lastrow = Range("B4").End(xlDown).Row Range("B5", "B" & lastrow).NumberFormatLocal = "yyyy/mm/dd" Range("A1").Select End Sub もうひとつ Sub GETデータ() With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Cells(lastrow, 2)) ↑ここにデバックで黄色になります。 .Name = "t?s=998407.o&g=d" .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 = "10" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Subになります。Excel2007です。

  • 自動Web取込み

    例えば、ゆうパック 都道府県別の基本運賃表「http://www.post.japanpost.jp/service/you_pack/charge/ichiran.html」の県名がハイパーリンクになっておりA3からA49にデータが入っているとします。 各県を自動的に開け、運賃「http://www.post.japanpost.jp/service/you_pack/charge/ichiran/01.html」をB53以降に取込みたいと思っております。 Sub sample() Dim h As Hyperlink For Each h In ActiveSheet.Hyperlinks h.Range.Offset(0, 1) = h.Address Next Dim i As Long Dim myAddress As String For i = 3 To 49 myAddress = Range("B" & i).Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & myAddress, Destination:=Range("B53")) .Name = "01" .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 = "1" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With Next i End Sub URL「http://www.post.japanpost.jp/service/you_pack/charge/ichiran/01.html」のコピーされたB4、B5と変化させる方法がうまくいきません。 宜しくお願い致します。

  • あと少しなんですが、時系列の次のページに関して

    yahoo時系列(http://table.yahoo.co.jp/t?c=2007&a=6&b=28&f=2007&d=9&e=29&g=d&s=1321.o&y=0&z=1321.o) にある、最初の50位までは下記で、できるのですが、51位以降のダウンロードができません。 Scripte Eitorで確認したところ、(1)(50位まで)が(2)(51位から)に変わる(最後に&amp;x=.csvが追加されます)のですが、下記をどのように変更したらよいのか解りません。 わかる方、よろしくお願いします。 (1) http://table.yahoo.co.jp/t?c=2007&a=4&b=29&f=2007&d=9&e=29&g=d&s=8411.t&y=0&z=8411.t (3) http://table.yahoo.co.jp/t?s=8411.t&a=4&b=29&c=2007&d=9&e=29&f=2007&g=d&q=t&y=100&z=8411.t&x=.csv ----------------------------- Dim kaisituki As Integer '選択開始月 Dim kaisibi As Integer '選択開始日 Dim shuuryoutuki As Integer '選択終了月 Dim shuuryoubi As Integer '選択終了日 Dim sijyou As Integer '選択市場 Dim sijyoukigou As String '選択市場記号 Range("d3").Select kaisituki = Range("b3") kaisibi = Range("b4") shuuryoutuki = Range("b5") shuuryoubi = Range("b6") sijyou = Range("b7") sijyoukigou = Range("b8") With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://table.yahoo.co.jp/t?c=2007&a=" & kaisituki & "&b=" & kaisibi & "&f=2007&d=" & shuuryoutuki & "&e=" & shuuryoubi & "&g=d&s=" & sijyou & "." & sijyoukigou & "&y=0&z=" & sijyou & "." & sijyoukigou & "" _ , Destination:=Range("d3")) .Name = _ "t?c=2007&a=" & kaisituki & "&b=" & kaisibi & "&f=2007&d=" & shuuryoutuki & "&e=" & shuuryoubi & "&g=d&s=" & sijyou & "." & sijyoukigou & "&y=0&z=" & sijyou & "." & sijyoukigou & """" .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 = "23" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With

  • VBAでWebクエリにて情報を自動収集するプログラム

    自動売買ロボット作成マニュアルという本を買いました。 これは株などを自動売買するプログラムを作るための方法が書いた本です。(言語はエクセルに搭載されてあるVBAというプログラム言語です) そのプログラムを作る過程で、まずYahoo!ファイナンスから株価などの情報を10年分自動収集するプログラムを作ったのですが、「インターネットサーバーに接続できません」と出て、きちんと実行できません。 そこで、デバックをすると.Refresh BackgroundQuery:=Falseというプログラムのところがチェックされました。どうしたらいいでしょうか? この文章だけでは対処できないと思いますのでプログラムを書いておきます。長々とお読みいただきありがとうございます。どうかお知恵をお貸しください。 Dim url As String Dim lastrow As Integer Dim i As Integer Sub Get_Data() With ActiveSheet.QueryTables.Add(Connection:=url, Destination:=Cells(lastrow, 2)) .Name = _ "t?s=998407.o&a=4&b=22&c=2008&d=7&e=24&f=2008&g=d&q=t&y=0&z=998407.o&x=_1" .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 = "19" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub Sub Calc() Dim code As String Dim data_length As Integer, date_temp As Date Dim day_s As Integer, month_s As Integer, year_s As Integer Dim day_e As Integer, month_e As Integer, year_e As Integer Dim row_length As Integer code = "998407.o" data_length = -3650 date_temp = DateAdd("d", data_length, Now) day_e = Day(Now) month_e = Month(Now) year_e = Year(Now) day_s = Day(date_temp) month_s = Month(date_temp) year_s = Year(date_temp) Range("B4:H65000").ClearContents For i = 0 To Abs(data_length) * 0.65 Step 50 url = "URL; http://table.yahoo.co.jp/t?s=" & code & "&a=" & month_s & "&b=" & day_s & "&c=" & year_s & "&d=" & month_e & "&e=" & day_e & "&f=" & year_e & "&g=d&q=t&y=" & i & "&z=" & code & "&x=.csv" If i = 0 Then lastrow = "4" Call Get_Data If Range("B4") = "" Then Exit Sub End If Else lastrow = (Range("B4").End(xlDown).Row + 1) Call Get_Data Range("B" & lastrow, "H" & lastrow).Delete row_length = (Range("B4").End(xlDown).Row) If row_length - lastrow < 49 Then Exit For End If End If Next Range("B5:H65000").Sort Key1:=Columns("B") lastrow = Range("B4").End(xlDown).Row Range("B5", "B" & lastrow).NumberFormatLocal = "yyyy/mm/dd" Range("C5", "H" & lastrow).NumberFormatLocal = "0" Range("A1").Select End Sub

  • マクロのWEBデータの取り込み

    下のように書いてマクロを実行したのですが(EXCEL2003) Sheet1のA1からDP1000のデータをクリアにしてから URLのデータを取り込んで D列の最終行の値を Sheet2のA列の日付+1行に書き込みたいのですが Sheet1のA1からDP1000のデータをクリアにする前に D列の最終行の値を Sheet2のA列の日付+1行に書き込んでしまいます。 あとConst Col = 4の部分が 同じ範囲内で宣言が重複しています。 とエラーになります。 どこが悪いのかがわかりません。 よろしくお願いします。 Sub 抽出() Worksheets("Sheet1").Range("A1:DP1000").Value = "" nen = InputBox("読み込む年度、西暦4桁(半角)読み込む月(半角)を入力") tuki = InputBox("読み込む日(半角)を入力") strUrl= "URL;http://○○○○★★★★DATFR=#01&DATTO=#$&MSCD=1431&BMCD=30&MENU_ID=2&MENU_ID1=2" strnen = Mid(Str(nen), 2) strtuki = Mid(Str(tuki), 2) strUrl = Replace(Replace(strUrl, "$", strtuki), "#", strnen) strName = Replace(Replace(strName, "$", strtuki), "#", strnen) With Worksheets("野菜").QueryTables.Add(Connection:=strUrl, Destination:=Worksheets("Sheet1").Range("A1")) .Name = strtuki .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=True End With Const Col = 4 Dim Rw As Long With Sheets("Sheet1") Rw = .Cells(Rows.Count, Col).End(xlUp).Row Sheets("Sheet2").Range("A" & tuki + 1 & ":A" & tuki + 1).Value = _ .Range(.Cells(Rw, 4), .Cells(Rw, 4)).Value End With strUrl= "URL;http://○○○○■■■■DATFR=#01&DATTO=#$&MSCD=1431&BMCD=30&MENU_ID=2&MENU_ID1=2" strnen = Mid(Str(nen), 2) strtuki = Mid(Str(tuki), 2) strUrl = Replace(Replace(strUrl, "$", strtuki), "#", strnen) strName = Replace(Replace(strName, "$", strtuki), "#", strnen) With Worksheets("Sheet3").QueryTables.Add(Connection:=strUrl, Destination:=Worksheets("Sheet3").Range("A1")) .Name = strtuki .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=True End With Const Col = 4 Dim Rw As Long With Sheets("Sheet3") Rw = .Cells(Rows.Count, Col).End(xlUp).Row Sheets("Sheet4").Range("A" & tuki + 1 & ":A" & tuki + 1).Value = _ .Range(.Cells(Rw, 4), .Cells(Rw, 4)).Value End With End Sub

  • マクロ VBA 簡単なことなのかもしれませんが?

    時系列をexcelにロードする際、http://table.yahoo.co.jp/t?c=2007&a=6&b=20&f=2007&d=9&e=21&g=d&s=1321.o&y=0&z=1321.o のページの 月日から月日までの部分と銘柄コード=excelシート入力済。⇒VBA⇒ロードするための作業を、下記の方法で、とりあえず、終了日のところから改式をやってみたのですが、うまくゆきません。 "選択終了日"に変更する前の、数字の状態ではロードできるので、テーブル番号等は、問題ないようです。 原因の解る方、よろしくお願いします。 ----------------------------- Sub test() Range("a1:h300") = "" Dim shuuryoubi As String '選択終了日 Range("b1").Select With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://table.yahoo.co.jp/t?c=2007&a=6&b=20&f=2007&d=9&e=選択終了日&g=d&s=1321.o&y=0&z=1321.o" _ , Destination:=Range("b1")) .Name = _ "t?c=2007&a=6&b=20&f=2007&d=9&e=選択終了日&g=d&s=1321.o&y=0&z=1321.o" .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 = "23" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False shuuryoubi = 17 End With End Sub

  • VBAでデータ更新が遅い

    こんにちは。 たびたびお世話になってます。 今、VBAでYahooファイナンスのサイトからWebクエリでシートに株価データを落として 手元にある、株価の入ったcsvファイルを更新する(新しいデータがあれば更新)マクロを 書いてるんですが、マクロ実行当初はまぁまぁの速さなのですが、株価データは大量に あるため、3000銘柄くらいダウンロードすると、段々と速度が落ちてしまいます。 速度が落ちない良い方法はないでしょうか。 csvはエクセルでオープンしてます。Open文でcsvをテキストとして開いた方が 良いんでしょうか。。とりあえず、Webクエリの部分だけですが、ご教示お願いします。 Sub WebStockGet(ByVal httpUrl As String, ByRef testWs As Worksheet) With testWs.QueryTables.Add(Connection:=httpUrl, Destination:=testWs.Range("A1")) .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlAllTables .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub -- エクセル2003

専門家に質問してみよう