2017-05-13 14 views
0

の特定のオプション値を引くために必要なVBAマクロ:私は現在、Webページをロードするには、次のマクロを持っているWebページ

Sub OOS_Query() 
'This together with the True value at the end will tell the macro to not update the screen until it reaches a point that I want it to show updates again 
Application.ScreenUpdating = False 
ActiveWorkbook.Connections("Connection1").Delete 
Sheet2.Range("A:C").Clear 
With Sheet2.QueryTables.Add(Connection:= _ 
"URL;http://[ommitted on purpose]id=42908", Destination:=Sheet2.Range("$A$1")) 
.FieldNames = True 
.PreserveFormatting = True 
.RefreshOnFileOpen = True 
.BackgroundQuery = True 
.RefreshStyle = xlInsertDeleteCells 
.RefreshPeriod = 5 
.WebSelectionType = xlSpecifiedTables 
.WebFormatting = xlWebFormattingNone 
.WebTables = "1,2" 
.WebPreFormattedTextToColumns = True 
.WebConsecutiveDelimitersAsOne = True 
.WebSingleBlockTextImport = False 
.WebDisableDateRecognition = False 
.WebDisableRedirections = False 
.Refresh BackgroundQuery:=True 

End With 
Application.ScreenUpdating = True 
End Sub 

あなたはWebページは常にそのように変化している「ID」の値を持って見ることができるようにデータベースに照会します。

Date <select name="id"> 
<option value='43032' >2017-05-13 05:00:01</option> 
<option value='43031' >2017-05-13 04:45:02</option> 
<option value='43030' >2017-05-13 04:30:01</option> 
<option value='43029' >2017-05-13 04:15:02</option> 

私は何idを持つウェブサイトを引っ張ることができるように、コードに統合する方法を探しています

<option value='43004' >2017-05-12 22:00:01</option> 

...:Webページのソースに次のようにその値が見えます時間が21:58:00から22:02:00の間である限り、それはあります。現在の日付が何であっても。これが通常行われる方法は、ウェブサイトにアクセスして、ドロップダウンメニューから検索したい日付/時刻を選択し、ウェブサイトを上記のコードのセクションに貼り付けることです。

私はそれを自動的に行うことができれば、コードを毎日編集する必要がなくなります。

ありがとうございます!

+0

22時:01にウェブサイトが表示される時間を選択しない理由は、時々変更されることがあるためです。 – Jahir

+0

DOMまたはXHRから取得したHTMLスニペットですか? – omegastripes

+0

@omegastripesそれはDOMです。 – Jahir

答えて

0

私はWebページをクエリするようにコードを調整しましたが、私の指定のシートのセルからID値を抽出しました。次に、コードにさらにいくつか追加しました。

私は毎日午後10時(22時間)にそれを必要としているので、IDは分かりやすいですし、そこにある値が何であれ、番号96が追加されることを知っています。96 = 24時間間隔で、15分ごとに変化する(1時間で4回)。だから私は24日に4回、私に96を与えました。

次に、上記のIDと日付を考慮したIDを持つ2つの列を作成します。それから私は探してIDの値を私に与える日に基づいて一致するダミーのセル上の配列の数式を構築した。コードは次のようになります。

Sub OOS_Query() 

Application.ScreenUpdating = False 
ActiveWorkbook.Connections("Connection1").Delete 
Sheet2.Range("A:C").Clear 

Dim wb As Workbook 
Dim src As Worksheet 
Dim url As String 
Dim symbol As String 

Set wb = ThisWorkbook 
Set src = wb.Sheets("OldTime") 
symbol = src.Range("K2") 
url = "URL;[omitted on purpose]=" 
url = url & symbol 

With Sheet2.QueryTables.Add(Connection:= _ 
url, _ 
Destination:=Sheet2.Range("$A$1")) 
.FieldNames = True 
.PreserveFormatting = True 
.RefreshOnFileOpen = True 
.BackgroundQuery = True 
.RefreshStyle = xlInsertDeleteCells 
.RefreshPeriod = 5 
.WebSelectionType = xlSpecifiedTables 
.WebFormatting = xlWebFormattingNone 
.WebTables = "1,2" 
.WebPreFormattedTextToColumns = True 
.WebConsecutiveDelimitersAsOne = True 
.WebSingleBlockTextImport = False 
.WebDisableDateRecognition = False 
.WebDisableRedirections = False 
.Refresh BackgroundQuery:=True 

End With 
Application.ScreenUpdating = True 
End Sub 

エクセル式:これは、同様の質問があるかもしれそこに誰も助け

INDEX(I:I,MATCH(TODAY(),J:J,0)) 

希望。

関連する問題