Convert HTML-table to Excel using VBA

后端 未结 9 2092
难免孤独
难免孤独 2021-02-05 02:51

Convert HTML-table to Excel

The code below fetches the HTML-table at https://rasmusrhl.github.io/stuff, and converts it to Excel-format.

The pr

9条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 03:01

    This works with a temp file.

    What it does: Downloads Data Locally. Then, replaces the "(" with a "\". Then, imports the data. Formats the data as text (to ensure we can change it back without error). Then, changes the text. This cannot be done with Range.Replace because that will reformat the cell contents.

    ' Local Variables
    Public FileName As String ' Temp File Path
    Public FileUrl As String ' Url Formatted Temp File Path
    Public DownloadUrl As String ' Where We're Going to Download From
    
    ' Declares Have to Be At Top
    Private Declare Function GetTempPath Lib "kernel32" _
      Alias "GetTempPathA" _
      (ByVal nBufferLength As Long, _
      ByVal lpBuffer As String) As Long
    Private Declare Function GetTempFileName Lib "kernel32" _
      Alias "GetTempFileNameA" _
      (ByVal lpszPath As String, _
      ByVal lpPrefixString As String, _
      ByVal wUnique As Long, _
      ByVal lpTempFileName As String) As Long
    
    ' Loads the HTML Content Without Bug
    Sub ImportHtml()
    
        ' Set Our Download URL
        DownloadUrl = "https://rasmusrhl.github.io/stuff"
    
        ' Sets the Temporary File Path
        SetFilePath
    
        ' Downloads the File
        DownloadFile
    
        ' Replaces the "(" in the File With "\(", We Will Later Put it Back
        ' This Ensures Formatting of Content Isn't Modified!!!
        ReplaceStringInFile
    
    
        ' Our Query Table is Now Coming From the Local File, Instead
        Dim s As QueryTable
        Set s = ActiveSheet.QueryTables.Add(Connection:=("FINDER;file://" + FileUrl), Destination:=Range("$A$1"))
    
        With s
    
            .Name = "stuff"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = False
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingAll
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = True
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
    
            ' Sets Formatting So When We Change Text the Data Doesn't Change
            .ResultRange.NumberFormat = "@"
    
            ' Loop Through Cells in Range
            ' If You Do Excel Replace, Instead It Will Change Cell Format
            Const myStr As String = "\(", myReplace As String = "("
            For Each c In .ResultRange.Cells
                Do While c.Value Like "*" & myStr & "*"
                    c.Characters(InStr(1, c.Value, myStr), Len(myStr)).Text = myReplace
                Loop
            Next
    
    
        End With
    End Sub
    
    ' This function replaces the "(" in the file with "\("
    Sub ReplaceStringInFile()
    
        Dim sBuf As String
        Dim sTemp As String
        Dim iFileNum As Integer
        Dim sFileName As String
    
        ' Edit as needed
        sFileName = FileName
    
        iFileNum = FreeFile
        Open sFileName For Input As iFileNum
    
        Do Until EOF(iFileNum)
            Line Input #iFileNum, sBuf
            sTemp = sTemp & sBuf & vbCrLf
        Loop
        Close iFileNum
    
        sTemp = Replace(sTemp, "(", "\(")
    
        iFileNum = FreeFile
        Open sFileName For Output As iFileNum
        Print #iFileNum, sTemp
        Close iFileNum
    
    End Sub
    
    ' This function sets file paths because we need a temp file
    Function SetFilePath()
    
        If FileName = "" Then
            FileName = GetTempHtmlName
            FileUrl = Replace(FileName, "\", "/")
        End If
    
    End Function
    
    ' This subroutine downloads the file from the specified URL
    ' The download is necessary because we will be editing the file
    Sub DownloadFile()
    
        Dim myURL As String
        myURL = "https://rasmusrhl.github.io/stuff"
    
        Dim WinHttpReq As Object
        Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
        WinHttpReq.Open "GET", DownloadUrl, False, "username", "password"
        WinHttpReq.send
    
        myURL = WinHttpReq.responseBody
        If WinHttpReq.Status = 200 Then
            Set oStream = CreateObject("ADODB.Stream")
            oStream.Open
            oStream.Type = 1
            oStream.Write WinHttpReq.responseBody
            oStream.SaveToFile FileName, 2 ' 1 = no overwrite, 2 = overwrite
            oStream.Close
        End If
    
    End Sub
    
    '''''''''''''''''''''''''''''
    ' THIS BLOCK OF CODE GETS A TEMPORARY FILE PATH USING THE GetTempHtmlName Function
    '''''''''''''''''''''''''''''
    
    
    Public Function GetTempHtmlName( _
      Optional sPrefix As String = "VBA", _
      Optional sExtensao As String = "") As String
      Dim sTmpPath As String * 512
      Dim sTmpName As String * 576
      Dim nRet As Long
      Dim F As String
      nRet = GetTempPath(512, sTmpPath)
      If (nRet > 0 And nRet < 512) Then
        nRet = GetTempFileName(sTmpPath, sPrefix, 0, sTmpName)
        If nRet <> 0 Then F = Left$(sTmpName, InStr(sTmpName, vbNullChar) - 1)
        If sExtensao > "" Then
          Kill F
          If Right(F, 4) = ".tmp" Then F = Left(F, Len(F) - 4)
          F = F & sExtensao
        End If
        F = Replace(F, ".tmp", ".html")
        GetTempHtmlName = F
      End If
    End Function
    
    '''''''''''''''''''''''''''''
    ' End - GetTempHtmlName
    '''''''''''''''''''''''''''''
    

提交回复
热议问题