Webクエリで困っています

このQ&Aのポイント
  • WebクエリでセルA1に入力したURLを読み込み、表を取り込んで必要な箇所をコピーしたいですが、エラーが発生しています。
  • マクロを使用して、Webクエリで特定のURLから表を取り込む方法を試していますが、エラーが発生しています。
  • Webクエリを使用してセルA1に入力したURLから表を取り込むためのマクロを作成しましたが、エラーが発生しています。
回答を見る
  • ベストアンサー

Webクエリで困っています

以下のマクロを作りました。Webクエリで、セルA1に入力してあるURLを読み込んで表を取り込み、必要なところを選択してコピーするようにしたいのです。しかし、実行すると上から3行目まで黄色になってエラーになります。特に3行目には矢印が表示されています。いろいろ調べましたが結局分かりませんでした。宜しくお願いします。 ------------------------------------------------------ Sub クエリで取得() ' ' クエリで取得 Macro ' マクロ記録日 : 2009/7/30 ユーザー名 : charlie ' ' With ActiveSheet.QueryTables.Add(Connection:= _ Range("A1").Value _ , Destination:=Range("A2")) .Name = "resultlist?tbws=x0p01a&hd=20090716&jcd=01_2" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = False .RefreshOnFileOpen = False .BackgroundQuery = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = False .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = True .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With Range("A19:Q54").Select Selection.Copy End Sub ----------------------------------------------------------

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

  • ベストアンサー
  • KenKen_SP
  • ベストアンサー率62% (785/1258)
回答No.2

下記で試してみて。 URL の簡易チェックで Like 演算子を使うため、大小文字の統一の 必要があり、Lcase 関数で URL を小文字化してたのが原因かも。 Sub クエリで取得()   Dim sConnectionStr As String   Dim sh     As Worksheet      Set sh = Worksheets("クエリで取得")      sConnectionStr = Trim$(sh.Range("A1").Text)      If Len(sConnectionStr) = 0 Then Exit Sub      If LCase$(sConnectionStr) Like "http://*" Or _     LCase$(sConnectionStr) Like "https://*" Then          sh.Rows("2:" & CStr(sh.Rows.Count)).ClearContents     sConnectionStr = "URL;" & sConnectionStr     With sh.QueryTables.Add(Connection:=sConnectionStr, _                 Destination:=sh.Range("A2"))       .FillAdjacentFormulas = False       .PreserveFormatting = False       .RefreshStyle = xlOverwriteCells       .AdjustColumnWidth = False       .RefreshPeriod = 0       .WebSelectionType = xlEntirePage       .WebFormatting = xlWebFormattingNone       .WebPreFormattedTextToColumns = True       .WebConsecutiveDelimitersAsOne = True       .WebSingleBlockTextImport = False       .WebDisableDateRecognition = True       .WebDisableRedirections = False       .Refresh BackgroundQuery:=False       .Delete     End With     sh.Range("A19:Q54").Copy   Else     MsgBox "A1 セルが未入力か URL として不適切", vbInformation   End If End Sub

974tkqkl
質問者

お礼

いやっ!できました!!! 感動です。イメージ通りのものができました。 感謝!感謝!感謝!です。 いろいろご親切にこちらのわがままを聞いて頂いて恐縮です。ありがとうございました。 いつも、『新しいマクロの記録』から作成して、不要な部分を削除する方法で作っていました。今回変数をはじめて知りました。いろいろなことができそうですね。難しそうですが、勉強してみます。 ありがとうございましたm(__)m

その他の回答 (1)

  • KenKen_SP
  • ベストアンサー率62% (785/1258)
回答No.1

QueryTables.Add(Connection:=~ に URL を指定する場合、URL であることを 表す "URL;" というキーワードが必要(下記ソースの※のライン)です。 また、不要と思われる各種プロパティー設定は多少整理しました。 下記ソースを VBE にコピー&ペーストすると http:// の部分で余計な「?」 記号までペーストされますが、削除して下さい。 Sub クエリで取得()      Dim sConnectionStr As String   Dim sh       As Worksheet      ' WEB クエリを作成するシートを参照   Set sh = ActiveSheet ' または Worksheets("シート名")      ' 前処理:余計なホワイトスペース等を除去し、Like 演算子で比較   ' できるように小文字化しておく   sConnectionStr = Trim$(LCase$(sh.Range("A1").Text))      ' A1 にはデータが入力されていて、かつ http:// で始まっているか   If Len(sConnectionStr) And sConnectionStr Like "http://*" Then          ' 前処理:一応シートをクリアした方が良いでしょう     sh.Rows("2:" & CStr(sh.Rows.Count)).ClearContents          ' ※POINT: 接続文字列に URL であることを表すキーワードを付与     sConnectionStr = "URL;" & sConnectionStr          ' WEB クエリを作成する     With sh.QueryTables.Add(Connection:=sConnectionStr, _                 Destination:=sh.Range("A2"))       .FillAdjacentFormulas = False       .PreserveFormatting = False       .RefreshStyle = xlOverwriteCells       .AdjustColumnWidth = False       .RefreshPeriod = 0       .WebSelectionType = xlEntirePage       .WebFormatting = xlWebFormattingNone       .WebPreFormattedTextToColumns = True       .WebConsecutiveDelimitersAsOne = True       .WebSingleBlockTextImport = False       .WebDisableDateRecognition = True       .WebDisableRedirections = False       ' 実際にインポートするには Refresh メソッドを実行する       ' 今回はインポートが完了するまで待機する必要があるので、       ' BackgroundQuery は False にしておく       .Refresh BackgroundQuery:=False       ' WEB クエリを繰り返し更新する必要がなければ       ' 削除した方が省リソースです       .Delete     End With          sh.Range("A19:Q54").Copy        Else     MsgBox "A1 セルが未入力か URL として不適切", vbInformation   End If End Sub

974tkqkl
質問者

補足

Sub クエリで取得() ' ' クエリで取得 Macro ' マクロ記録日 : 2009/8/1 ユーザー名 : charlie ' Dim sConnectionStr As String Dim sh As Worksheet Set sh = Worksheets("クエリで取得") sConnectionStr = Trim$(LCase$(sh.Range("A1").Text)) If Len(sConnectionStr) And sConnectionStr Like "https://*" Then sh.Rows("2:" & CStr(sh.Rows.Count)).ClearContents sConnectionStr = "URL;" & sConnectionStr With sh.QueryTables.Add(Connection:=sConnectionStr, _ Destination:=sh.Range("A2")) .FillAdjacentFormulas = False .PreserveFormatting = False .RefreshStyle = xlOverwriteCells .AdjustColumnWidth = False .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = True .WebDisableRedirections = False .Refresh BackgroundQuery:=False .Delete End With sh.Range("A19:Q54").Copy Else MsgBox "A1 セルが未入力か URL として不適切", vbInformation End If End Sub ------------------- 上記のように修正しましたが、 .Refresh BackgroundQuery:=Falseで黄色のエラーになります。 どうしたらよいでしょうか。 尚、http://をhttps://にしました。 宜しくお願いします。

関連するQ&A

  • エクセルWEBクエリ 貼り付け時の自動変換

    WEBクエリにて、データをシートに張り付ける際、 データの1:1 1:2 3:2などのコロンが間にある文字列が、 ユーザー定義の時間として認識して貼り付けされてしまい困っております。(h:mm) (例) 1:1 1:2 3:2 → 1:01 1:02 3:02 一通り、ヘルプやVBAのヘルプなどを見ましたが、わかりませんでした。 ハイフンで区切られているものは、WEBクエリ中のオプションで日付認識無効をチェックすることで、解決出来るようですが、コロン「:」の解決策は見当たりません。m(__)m もしご存知の方がおられたら、ご教授のほどお願い申し上げます。 そのままの数字が認識できれば、どんな方法でも構いません。 下記は、参考にWEBクエリのマクロ記録そのままの状態です。 Sub Macro5() With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://www.○○○", Destination:=Range("$A$1") ) .Name = "1122" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "12" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = True .WebDisableDateRecognition = True .WebDisableRedirections = True .Refresh BackgroundQuery:=False End With End Sub

  • EXCELのwebクエリについての質問です。

    EXCELのwebクエリについての質問です。 よろしくお願いします。 webサイトにあるデータをエクセルに自動で取り込みたいのですが、 そのサイトが、データ100件毎にページを増やしていく仕様なので50ページあったら50回 も手動でデータを取りにいかないといけません。なんとかして自動にしたいのですが・・・ そのサイトはhttp://○×○×.com/page=1/のように"page="に数値を入れれば良いのでエクセルでなんとかなりそうかなと思い質問しました。 ウェブクエリ取得をマクロで記録してみました。 With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://○×○×.com/page=1", Destination _ :=Range("$A$1")) .Name = "page=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 = "2" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With この中で、 1."page=1"を連番で連続取得したい。(総ページ数は人的に確認して、どこかに入力できればいいです) 2.エクセルに1つの表にしたいので:=Range("$A$1")だと上書きしていってしまのでどうにかしたい。 3..Name = "page=1"も連番で増やしていきたい。 こんなことは可能でしょうか。 VBAは初心者以下です。 どなたかお助けください。

  • エクセルマクロ WEBクエリを使用。WEBのURLを一部変数にし順次データを表示させたいのですが、うまくいきません。

    WEBクエリでWEB画面上の一部の情報をエクセル上に表示させます。 その際、URLの一部を変更して、順次新しいデータを表示させたいのです。 具体的にはURLに数字があり、その数字を変数にし、エクセル上にある複数の数字を順次読ませて次々表示させるイメージです。 まず「マクロの記録」を使って、WEBクエリを読ませるベースを作りました。 例として、yahooファイナンスを使ったケースを用いますと ---------------------------------------------------- Sub Macro1() ' Macro1 Macro With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://quote.yahoo.co.jp/q?s=6758.t&d=t", Destination:=Range("$A$1")) .Name = "q?s=6758.t&d=t" .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 ---------------------------------------------------- とマクロが書かれました。 この中の6758は証券コードで、別の証券コードにしたときの情報を取得したいため、6758を変数にしました。 ---------------------------------------------------- Dim SIC As Integer SIC = Range("A1").Text  ---------------------------------------------------- (注)Range("A1")に6758という数値が入っています この2文を上記ベースマクロの冒頭に加え、 ベースマクロ内の"6758"(2箇所)を"SIC"に打ちかえたら、 うまく読んでこないのです。 読ませるにはどうしたらよいでしょう。 素人なため、質問内容がわかりにくかったらご容赦ください。 わかりやすく教えて頂けるとありがたいです。 よろしくお願いします。

  • VBA Webクエリについて

    VBA初心者です。 VBAを使い、下記「やりたいこと」をしたいのですが、 「質問内容」にありますように、上手くできません…。 基礎がわかっていない為、お粗末な内容かもしれませんが、 教えてください。何卒よろしくお願い致します。 ■やりたいこと シート「1」にあるURLアドレスのリスト(C列)に沿って WEBクエリを実行し、シート「2」に取得したデータを 連続して末行に入力したいです。 ■質問内容 見よう見真似で、下記VBAを作成しました。(1To3は仮の回数です) WEBクエリが実行され、末行に取得データが入力されますが、 1回目、2回目の取得データは消え(長い空白行ができ)、 ラストの3回目のデータのみが残るかたちになります。 取得したデータが消えず、連続してWEBクエリを実行できるよう 下記を添削して頂けませんでしょうか。 ---------------------------------------------------------------- Sub Macro2() Dim Lrow As Long For カウンタ = 1 To 3 Lrow = Worksheets("2").Range("A" & CStr(Rows.Count)).End(xlUp).Row セル範囲 = "C" & カウンタ With ActiveSheet.QueryTables.Add(Connection:= _ "URL;" & Worksheets("1").Range(セル範囲).Value, Destination:=Worksheets("2").Range("A" & Lrow)) .Name = Worksheets("1").Range(セル範囲).Value .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:=False End With Next End Sub

  • エクセル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

  • 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

  • マクロの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

  • 連続してWEBクエリを実行

    VBA初心者です。(作業環境Windows7 64bit Excel2010) 現在、WEBクエリを使用し、あるホームページより情報を収集する作業を行っているのですが、 WEBクエリをループさせる技術がない為、作業が難航しております。 具体的に行いたい作業としましては、 A列に商品番号を数種類入れておき、VBAにてURLの後ろにその番号を追加、 つなぎ合わせたURLでホームページにアクセス ⇒ 情報を収集するというものです。 乏しい知識をフル活用し、ループさせずに情報を収集するところまでは作成できたのですが、 どちら様か、ループさせる方法を教えていただける方がおりましたら、ご指導いただけないでしょうか。 ■セルに予め入力する番号例■       A 【1】 131023999 【2】 131022082 【3】 131023869 【4】 131023796 【5】 131044236 ※最終的には、一度に100種類のページにアクセスしたいと考えております。 ■現在作成しているVBAサンプル■ Sub WEBクエリ実行() Dim SIC1 As String Dim SIC2 As String Dim SIC3 As String SIC1 = Range("A1").Text SIC2 = Range("A2").Text SIC3 = Range("A3").Text On Error Resume Next With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.bcn-chubu.jp/search/detailed.php?id=" & SIC1, Destination:=Range("C1")) .FieldNames = True .FillAdjacentFormulas = False .PreserveFormatting = True .BackgroundQuery = True .SaveData = True .AdjustColumnWidth = True .WebTables = "1" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .Refresh BackgroundQuery:=False End With With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.bcn-chubu.jp/search/detailed.php?id=" & SIC2, Destination:=Range("C31")) .FieldNames = True .FillAdjacentFormulas = False .PreserveFormatting = True .BackgroundQuery = True .SaveData = True .AdjustColumnWidth = True .WebTables = "1" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .Refresh BackgroundQuery:=False End With With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.bcn-chubu.jp/search/detailed.php?id=" & SIC3, Destination:=Range("C61")) .FieldNames = True .FillAdjacentFormulas = False .PreserveFormatting = True .BackgroundQuery = True .SaveData = True .AdjustColumnWidth = True .WebTables = "1" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .Refresh BackgroundQuery:=False End With End Sub ちなみに、WEBクエリで収集した情報は、それぞれ指定のセルにアウトプットさせております。 ※30行刻みで情報が書き出されるようになっております。 VBA初心者のため、現在作成しているプログラムが「正しいのか」さえ、判断できていない状況ですが、ご指導いただける方がおりましたら、何卒よろしくお願いいたします。

  • 自動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と変化させる方法がうまくいきません。 宜しくお願い致します。

  • WEBクエリ

    WEBクエリでヤフーから、時系列で株価を取得するマクロを作っています。 シートAの左に、次々と1銘柄1シートで読み込む場合、下のコードをもっと短くする方法はあるのでしょうか?コードは同じで、銘柄の番号のみ違います。よろしくお願いします。 Sheets("A").Select Sheets.Add ActiveSheet.Name = "2002" With ActiveSheet.QueryTables.Add(Connection:="URL;http://table.yahoo.co.jp/t?s=2002.T&g=d", Destination:=Range("A2")) .Refresh BackgroundQuery:=False End With Range("A1").Select ActiveCell.FormulaR1C1 = "2002" Sheets("A").Select Sheets.Add ActiveSheet.Name = "2202" With ActiveSheet.QueryTables.Add(Connection:="URL;http://table.yahoo.co.jp/t?s=2202.T&g=d", Destination:=Range("A2")) .Refresh BackgroundQuery:=False End With Range("A1").Select ActiveCell.FormulaR1C1 = "2202" Sheets("A").Select Sheets.Add ActiveSheet.Name = "3000" With ActiveSheet.QueryTables.Add(Connection:="URL;http://table.yahoo.co.jp/t?s=3000.T&g=d", Destination:=Range("A2")) .Refresh BackgroundQuery:=False End With Range("A1").Select ActiveCell.FormulaR1C1 = "3000" ・ ・ ・ このような感じで銘柄の番号のみが違うコードが続きます。

専門家に質問してみよう