質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

Q&A

2回答

4243閲覧

【Excel VBA】乗換案内の検索結果のURLを転記したい

m.harada

総合スコア10

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

0グッド

0クリップ

投稿2019/06/02 08:29

編集2019/06/02 09:28

本サイトを使うのは初めてで、VBAに関しても初心者なのですがよろしければ教えて頂きたいです。

前提・実現したいこと

http://henkan.tokyo/article/461532061.html
上記のサイトの 「【Excel VBA】「Yahoo路線情報」の乗り換え案内から、複数分の移動時間・運賃を一括検索する」 というVBAを丸々使わせて頂いてるのですが、検索結果の時間、料金に加えQ列にその入力して出た検索結果のURLを転記したいです。
イメージ説明

エラーメッセージ

該当のソースコード

Option

1Dim colSh As Object 2Dim win As Object 3Dim strTemp As String 4Dim IE As InternetExplorer 5Dim objIE(5) As InternetExplorer 6Dim objIE2 As InternetExplorer 7Dim txtInput(16) As HTMLInputElement 8Dim txtInput2(20) As HTMLSelectElement 9Dim button(10) As HTMLInputElement 10Dim Form 11Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 12 13Sub 集計() 14Dim x As Long 15IE立ち上げ 16x = 4 17Do Until x > Cells(60000, 3).End(xlUp).Row 18乗り換え案内検索 (x) 19乗り換え案内結果集計 (x) 20IE繰り返し 21x = x + 1 22Loop 23IE終了 24End Sub 25 26Private Sub IE立ち上げ() 27'①InternetExplorerを開く 28Set IE = CreateObject("InternetExplorer.application") 29IE.Visible = True 30'②Yahoo乗り換え検索のページ("http://transit.yahoo.co.jp/")に移動 31IE.navigate ("http://transit.yahoo.co.jp/") 32Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE 33DoEvents 34Sleep 1 35Loop 36Sleep 1000 37End Sub 38 39Private Sub 乗り換え案内検索(i As Long) 40Set colSh = CreateObject("Shell.Application") 41For Each win In colSh.Windows 42If TypeName(win.document) = "HTMLDocument" Then 43If InStr(win.document.Title, "乗換案内") > 0 Then 44Set objIE(0) = win 45Exit For 46End If 47End If 48Next 49'③出発地・目的地・時間・その他新幹線を使用するか、特急を使用するかなどの条件入力 50'③-1 出発地の入力 51Set txtInput(1) = objIE(0).document.getElementById("sfrom") 52txtInput(1).Value = Cells(i, 3).Value 53'③-2 到着地の入力 54Set txtInput(2) = objIE(0).document.getElementById("sto") 55txtInput(2).Value = Cells(i, 4).Value 56'③-3 新幹線を利用するか 57Set txtInput(3) = objIE(0).document.getElementById("sexp") 58If Cells(i, 8).Value = "○" Then 59txtInput(3).Checked = True 60Else 61txtInput(3).Checked = False 62End If 63'③-4 有料特急を利用するか 64Set txtInput(4) = objIE(0).document.getElementById("exp") 65If Cells(i, 9).Value = "○" Then 66txtInput(4).Checked = True 67Else 68txtInput(4).Checked = False 69End If 70'③-5 出発時間での検索 71Set txtInput(5) = objIE(0).document.getElementById("tsDep") 72If Cells(i, 5).Value = "出発" Then 73txtInput(5).Checked = True 74End If 75'③-6 到着時間での検索 76Set txtInput(6) = objIE(0).document.getElementById("tsArr") 77If Cells(i, 5).Value = "到着" Then 78txtInput(6).Checked = True 79End If 80'③-7 日時の入力 81Set txtInput2(0) = objIE(0).document.getElementById("y") 82txtInput2(0).Value = Year(Cells(i, 6)) 83Set txtInput2(1) = objIE(0).document.getElementById("m") 84txtInput2(1).Value = Format(Month(Cells(i, 6)), "00") 85Set txtInput2(2) = objIE(0).document.getElementById("d") 86txtInput2(2).Value = Format(Day(Cells(i, 6)), "00") 87Set txtInput2(3) = objIE(0).document.getElementById("hh") 88txtInput2(3).Value = Format(Hour(Cells(i, 7)), "00") 89Set txtInput2(4) = objIE(0).document.getElementById("mm") 90txtInput2(4).Value = Format(Minute(Cells(i, 7)), "00") 91'③-8 検索結果表示順の選択 92Set txtInput2(5) = objIE(0).document.getElementsByName("s")(0) 93If Cells(i, 10).Value = "到着が早い順" Then 94txtInput2(5).Value = "0" 95ElseIf Cells(i, 10).Value = "乗り換え回数順" Then 96txtInput2(5).Value = "2" 97ElseIf Cells(i, 10).Value = "料金が安い順" Then 98txtInput2(5).Value = "1" 99End If 100'④検索ボタンをクリック 101Set Form = objIE(0).document.getElementById("searchModuleSubmit") 102Form.Click 103Sleep 1000 104Do While objIE(0).Busy Or objIE(0).readyState <> READYSTATE_COMPLETE 105DoEvents 106Sleep 1 107Loop 108Sleep 1000 109End Sub 110 111Sub 乗り換え案内結果集計(i As Long) 112Set colSh = CreateObject("Shell.Application") 113For Each win In colSh.Windows 114If TypeName(win.document) = "HTMLDocument" Then 115If InStr(win.document.Title, "乗換案内") > 0 Then 116Set objIE(0) = win 117Exit For 118End If 119End If 120Next 121'⑤表示されたページから必要な情報を収集 122Set txtInput(7) = objIE(0).document.getElementsByClassName("small")(0) 123Cells(i, 11).Value = txtInput(7).innerText 124Cells(i, 11).Value = Replace(Replace(Cells(i, 11), "(", ""), ")", "") 125Set txtInput(8) = objIE(0).document.getElementsByClassName("small")(1) 126Cells(i, 13).Value = txtInput(8).innerText 127Cells(i, 13).Value = Replace(Replace(Cells(i, 13), "(", ""), ")", "") 128Set txtInput(9) = objIE(0).document.getElementsByClassName("small")(2) 129Cells(i, 15).Value = txtInput(9).innerText 130Cells(i, 15).Value = Replace(Replace(Cells(i, 15), "(", ""), ")", "") 131Set txtInput(10) = objIE(0).document.getElementsByClassName("fare")(0) 132Cells(i, 12).Value = txtInput(10).innerText 133Set txtInput(11) = objIE(0).document.getElementsByClassName("fare")(1) 134Cells(i, 14).Value = txtInput(11).innerText 135Set txtInput(12) = objIE(0).document.getElementsByClassName("fare")(2) 136Cells(i, 16).Value = txtInput(12).innerText 137End Sub 138 139Private Sub IE繰り返し() 140Set colSh = CreateObject("Shell.Application") 141For Each win In colSh.Windows 142If TypeName(win.document) = "HTMLDocument" Then 143strTemp = win.document.body.innerText 144If InStr(strTemp, "乗換案内") > 0 Then 145Set objIE(0) = win 146Exit For 147End If 148End If 149Next 150'⑥-1 Yahoo乗り換え検索のページ("http://transit.yahoo.co.jp/")に移動 151objIE(0).navigate ("http://transit.yahoo.co.jp/") 152Sleep 1000 153Do While objIE(0).Busy Or objIE(0).readyState < READYSTATE_COMPLETE 154DoEvents 155Sleep 1 156Loop 157Sleep 1000 158End Sub 159 160Private Sub IE終了() 161Set colSh = CreateObject("Shell.Application") 162For Each win In colSh.Windows 163If TypeName(win.document) = "HTMLDocument" Then 164strTemp = win.document.body.innerText 165If InStr(strTemp, "乗換案内") > 0 Then 166Set objIE(0) = win 167Exit For 168End If 169End If 170Next 171'⑦InternetExplorerを終了 172objIE(0).Quit 173End Sub 174

試したこと

初心者なので検索しながら色々試してみたのですが、検索結果のURLをどうExcelと紐づけて転記すればいいかわかりません。
コード内のコメントだけでなんとなく流れを把握してる状態です。
試した事は

Set txtInput(7) = objIE(0).document.getElementsByClassName("small")(0) Cells(i, 11).Value = txtInput(7).innerText Cells(i, 11).Value = Replace(Replace(Cells(i, 11), "(", ""), ")", "") Set txtInput(8) = objIE(0).document.getElementsByClassName("small")(1) Cells(i, 13).Value = txtInput(8).innerText Cells(i, 13).Value = Replace(Replace(Cells(i, 13), "(", ""), ")", "") Set txtInput(9) = objIE(0).document.getElementsByClassName("small")(2) Cells(i, 15).Value = txtInput(9).innerText Cells(i, 15).Value = Replace(Replace(Cells(i, 15), "(", ""), ")", "") Set txtInput(10) = objIE(0).document.getElementsByClassName("fare")(0) Cells(i, 12).Value = txtInput(10).innerText Set txtInput(11) = objIE(0).document.getElementsByClassName("fare")(1) Cells(i, 14).Value = txtInput(11).innerText Set txtInput(12) = objIE(0).document.getElementsByClassName("fare")(2) Cells(i, 16).Value = txtInput(12).innerText End Sub ```、 ここの部分で検索結果をExcelに転記しているのかなと思い、URLに変えてやろうと思ったのですが検索結果のURLの部分のクラス名すら見つけれず。。 そもそもこの部分ではないのでしょうか?付け加えるべきとこも分からないぐらい初心者です。 もしよろしければヒントでも良いので教えて頂けると嬉しいです。 ### 補足情報(FW/ツールのバージョンなど)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sobue

2019/06/02 08:53 編集

試した事を記載してください。 あと、参考としてソースを流用するのはいいですが、インデントを整形した方が読みやすいです。 自分で解析してないと思われて、解答もらいづらくなりますよ。
m.harada

2019/06/02 09:16

申し訳ございません。初心者なので多くの事がわかりません。 コード内のコメントだけでなんとなく流れを把握してる状態です。 試した事は、 '⑤表示されたページから必要な情報を収集 Set txtInput(7) = objIE(0).document.getElementsByClassName("small")(0) Cells(i, 11).Value = txtInput(7).innerText Cells(i, 11).Value = Replace(Replace(Cells(i, 11), "(", ""), ")", "") Set txtInput(8) = objIE(0).document.getElementsByClassName("small")(1) Cells(i, 13).Value = txtInput(8).innerText Cells(i, 13).Value = Replace(Replace(Cells(i, 13), "(", ""), ")", "") Set txtInput(9) = objIE(0).document.getElementsByClassName("small")(2) Cells(i, 15).Value = txtInput(9).innerText Cells(i, 15).Value = Replace(Replace(Cells(i, 15), "(", ""), ")", "") Set txtInput(10) = objIE(0).document.getElementsByClassName("fare")(0) Cells(i, 12).Value = txtInput(10).innerText Set txtInput(11) = objIE(0).document.getElementsByClassName("fare")(1) Cells(i, 14).Value = txtInput(11).innerText Set txtInput(12) = objIE(0).document.getElementsByClassName("fare")(2) Cells(i, 16).Value = txtInput(12).innerText End Sub ここの部分で検索結果をExcelに転記しているのかなと思い、URLに変えてやろうと思ったのですが検索結果のURLの部分のクラス名すら見つけれず。。 そもそもこの部分ではないのでしょうか?付け加えるべきとこも分からないぐらい初心者です。
sobue

2019/06/02 09:18

質問欄が修正できるので質問欄に記載ください。
m.harada

2019/06/02 09:22

分かりました。ありがとうございます。
sobue

2019/06/02 09:24

何度もすみません。 コードは最初にやっってもらってたように隠れるようにしてください。
m.harada

2019/06/02 09:29

申し訳ございません。承知しました。
sobue

2019/06/02 09:37 編集

あと、この質問では訂正はお任せしますが、インデントを整形してほしいです。 【例】 Private Sub IE終了() Set colSh = CreateObject("Shell.Application") For Each win In colSh.Windows If TypeName(win.document) = "HTMLDocument" Then strTemp = win.document.body.innerText If InStr(strTemp, "乗換案内") > 0 Then Set objIE(0) = win Exit For End If End If Next '⑦InternetExplorerを終了 objIE(0).Quit End Sub ここでは、インデント変わらなかったので新たに回答を生みました。
guest

回答2

0

あと、この質問では訂正はお任せしますが、インデントを整形してほしいです。
【例】

VBA

1Private Sub IE終了() 2 Set colSh = CreateObject("Shell.Application") 3 For Each win In colSh.Windows 4 If TypeName(win.document) = "HTMLDocument" Then 5 strTemp = win.document.body.innerText 6 If InStr(strTemp, "乗換案内") > 0 Then 7 Set objIE(0) = win 8 Exit For 9 End If 10 End If 11 Next 12 '⑦InternetExplorerを終了 13 objIE(0).Quit 14End Sub

投稿2019/06/02 09:36

sobue

総合スコア329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

URLの取得
取得したものをcellに出力してあげたらいいと思います。
検索結果の時間に関しては、もう少しお待ちください。

投稿2019/06/02 09:03

sobue

総合スコア329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sobue

2019/06/02 09:32

開発者ツールはご存じでしょうか? 一度、中身を見てみるとイメージがわくかもしれません。
sobue

2019/06/02 09:43

料金や乗車時間の取得元は試したところで合っています。
m.harada

2019/06/02 09:52

ありがとうございます。見てみます。 objIE(0).urlでURLを取得して、どのようにG列に転記すればよいのかわかりません。
m.harada

2019/06/02 10:01

ありがとうございます。 試してみます。
m.harada

2019/06/02 10:13

ありがとうございます。
m.harada

2019/06/02 10:28

今日はちょっともう触れないので明日また勉強させて頂きます。ありがとうございます。
m.harada

2019/06/03 04:14

日またぎで申し訳ございません。 objIE(0).urlで取得したものの出力はどこに入れれば良いのでしょうか? 運賃、時間等が出力されているとこに入れてみたのですが出来ませんでした。
sobue

2019/06/03 14:19

デバッグはしましたか? どうだめだったのか具体的にわかりますか? すみません。しばらく自分で環境作って確認できませんので詳しい情報が欲しいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問