Sunteți pe pagina 1din 7

Attribute VB_Name = "Module6"

Option Explicit

Sub importhn()

Dim Fileout As Object


Dim fso As Object

Dim dt As Date
Dim sheet As Worksheet
Dim arr

Dim nr, br50, i, hnfn, a, hi, hni, brc, noh, dd, mm, yyyy As Integer

Dim pd, nf, lf, hnwa, rhn As String

Set fso = CreateObject("Scripting.FileSystemObject")

Dim ie As Object
Dim url As String
'Dim ie As InternetExplorer
Dim html As HTMLDocument

Dim hn(88)

Dim objFSO As Object


Dim objFolder As Object
Dim objSubFolder As Object

Set fso = CreateObject("Scripting.FileSystemObject")

'Remember time when macro starts


Sheets("Main").Select
pd = Range("J2").Value

Set objFSO = CreateObject("Scripting.FileSystemObject")


'Get the folder object
Set objFolder = objFSO.GetFolder(pd & "")

' For Each objSubFolder In objFolder.subfolders


' RmDir objSubFolder
'Next

MkDir (pd & "3 Horse Data Section 1")

dt = Sheets("Main").Range("c2").Value

dd = Day(dt)
If dd < 10 Then dd = "0" & dd
mm = Month(dt)
If mm < 10 Then mm = "0" & mm

yyyy = Year(dt)
For Each sheet In ActiveWorkbook.Worksheets
If sheet.Name = "Temp2" Then Worksheets("Temp2").Delete
Next sheet

Sheets.Add After:=ThisWorkbook.Sheets(4)
ActiveSheet.Name = "Temp2"

'<---import HMTL

'open Internet Explorer in memory, and go to website


Set ie = New InternetExplorer
ie.Visible = False 'True
ie.navigate "https://www.racingpost.com/racecards/" & yyyy & "-" & mm & "-" & dd &
"/runners-index"

'Wait until IE is done loading page


Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop

Application.Wait (Now + TimeValue("0:00:28"))

'show text of HTML document returned


Set html = ie.document

arr = Split(html.DocumentElement.outerHTML, vbLf) 'or vbCR or vbCrLf

Range("A1").Resize(UBound(arr) + 1, 1).Value = Application.Transpose(arr)

ie.Quit

'<----move to top & delete any duplications

'Application.Bar = "3/4 Completed 20%"

'Name "Z:\Binary\200.txt" As "Z:\Binary\220.txt"

hni = 0

brc = 0

Sheets("VICAR").Select
noh = Range("n1").Value
hi = 0

lf = pd & "3 Horse Data Section 1"

Range("a2").Select
Do Until ActiveCell.Value = ""
hi = hi + 1
'Application.StatusBar = "3/4 Completed " & Int(hi / noh * 75) + 20 & "%"

'MsgBox ("Z:\Binary\" & lf & ".txt")


'MsgBox ("Z:\Binary\2" & Int(hi / noh * 75) & ".txt")

If hi / noh > 1 Then a = 0 / 0

Sheets("VICAR").Select
rhn = LCase(Trim(ActiveCell.Value))

hnfn = rhn

For i = 1 To Len(rhn)
If Mid(rhn, i, 1) = " " Then rhn = Left(rhn, i - 1) & "-" & Right(rhn, Len(rhn) -
i)
Next i

Sheets("Temp2").Select

Cells.Find(What:="/" & rhn, After:=ActiveCell, LookIn:=xlFormulas, LookAt _


:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate

a = ActiveCell.Value

i = 1
Do Until Mid(a, i, 26) = "js-bestOddsRunnerHorseName" Or i > Len(a)
i = i + 1
Loop

If i > Len(a) Then a = 0 / 0

hnwa = "https://www.racingpost.com/" & Mid(a, i + 34, 30 + Len(rhn))

Do Until Right(hnwa, 1) = " "


hnwa = Left(hnwa, Len(hnwa) - 1)
Loop

hnwa = Left(hnwa, Len(hnwa) - 2)

hnwa = Trim(hnwa)

Sheets(hnfn).Select
Cells.Select
Selection.NumberFormat = "@"

nf = pd & "3 Horse Data Section 2 " & Int(hi / noh * 100) & "% attempt " & nr

Name lf As nf

lf = nf

If Range("a42").Value = "" Then

Range("A1").Select

Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False 'True
.navigate hnwa ' should work for any URL
Do Until .readyState = 4: DoEvents: Loop
End With

Application.Wait (Now + TimeValue("0:00:08"))

ie.ExecWB 17, 0 '// SelectAll


ie.ExecWB 12, 2 '// Copy selection
Range("A1").Select

ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False


Application.CutCopyMode = False
' Application.CutCopyMode = False
ie.Quit

' Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select

' Do Until Len(Trim(ActiveCell.Value)) > 0


' ActiveCell.Offset(-1, 0).Select
' Loop

Range("a1").Select
br50 = 0

Do Until br50 > 250

ActiveCell.Value = Trim(ActiveCell.Value)

ActiveCell.Value = Application.WorksheetFunction.Clean(ActiveCell.Value)

'MsgBox (Len(Trim(ActiveCell.Value)))
If Len(Trim(ActiveCell.Value)) > 0 Then
br50 = 0
ActiveCell.Offset(1, 0).Select
End If

If Len(Trim(ActiveCell.Value)) = 0 Then
br50 = br50 + 1
Rows(ActiveCell.Row).Select
Selection.Delete Shift:=xlUp
Range("a" & ActiveCell.Row).Select
End If

' Sheets("VICAR").Select

Loop
Range("a42").Select

nr = 0
Do Until Range("a42").Value <> "" Or nr > 10
nr = nr + 1
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False 'True
.navigate hnwa ' should work for any URL
Do Until .readyState = 4: DoEvents: Loop
End With
For i = 1 To nr
Application.Wait (Now + TimeValue("0:00:08"))
Next i
ie.ExecWB 17, 0 '// SelectAll
ie.ExecWB 12, 2 '// Copy selection
Range("A1").Select

ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False


Application.CutCopyMode = False
ie.Quit

Range("a1").Select
br50 = 0

Do Until br50 > 250

ActiveCell.Value = Trim(ActiveCell.Value)

ActiveCell.Value = Application.WorksheetFunction.Clean(ActiveCell.Value)

'MsgBox (Len(Trim(ActiveCell.Value)))
If Len(Trim(ActiveCell.Value)) > 0 Then
br50 = 0
ActiveCell.Offset(1, 0).Select
End If

If Len(Trim(ActiveCell.Value)) = 0 Then
br50 = br50 + 1
Rows(ActiveCell.Row).Select
Selection.Delete Shift:=xlUp
Range("a" & ActiveCell.Row).Select
End If

'Sheets("VICAR").Select

nf = pd & "3 Horse Data Section 2 " & Int(hi / noh * 100) & "% attempt " & nr

Name lf As nf

lf = nf

Loop

If Range("a42").Value = "" Then a = 0 / 0


Loop

If nr > 10 Then a = 0 / 0

End If

Sheets("VICAR").Select
ActiveCell.Offset(1, 0).Select

Loop
'Application.StatusBar = "3/4 Completed 99%"

Sheets("Temp2").Delete

End Sub
Sub manu()

Range("A1").Select

Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False 'True
.navigate hnwa ' should work for any URL
Do Until .readyState = 4: DoEvents: Loop
End With

Application.Wait (Now + TimeValue("0:00:28"))

ie.ExecWB 17, 0 '// SelectAll


ie.ExecWB 12, 2 '// Copy selection
Range("A1").Select

ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False


Application.CutCopyMode = False
' Application.CutCopyMode = False
ie.Quit

' Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select

' Do Until Len(Trim(ActiveCell.Value)) > 0


' ActiveCell.Offset(-1, 0).Select
' Loop

'Name ThisWorkbook.Path & "/" & lf & ".txt" As ThisWorkbook.Path & "/299.txt"

Range("a1").Select
br50 = 0

Do Until br50 > 250

ActiveCell.Value = Trim(ActiveCell.Value)

ActiveCell.Value = Application.WorksheetFunction.Clean(ActiveCell.Value)

'MsgBox (Len(Trim(ActiveCell.Value)))
If Len(Trim(ActiveCell.Value)) > 0 Then
br50 = 0
ActiveCell.Offset(1, 0).Select
End If

If Len(Trim(ActiveCell.Value)) = 0 Then
br50 = br50 + 1
Rows(ActiveCell.Row).Select
Selection.Delete Shift:=xlUp
Range("a" & ActiveCell.Row).Select
End If
Loop

nf = pd & "3 Horse Data Completed"

Name lf As nf

End Sub

S-ar putea să vă placă și