0

So I have a situation in which I need to populate a specific column from an URL ( Basically a get request to that URL will give a list of numbers ).

I am new with the macros but I want to make a system in which I will click a button and then that will read the data and populate the column.

Edit:

I have a URL, let's say 161.202.176.187/importData.txt and it has the following data.

12345
67890
12345
09876
87653
14214
14566
46131
12456
35098

Now in my excel, I have a button like this :

enter image description here

and I need to populate the column A1 with the data.

5
  • I think you have to provide more details ... Commented Mar 22, 2018 at 11:25
  • 1
    Please consult this for a minimal example of what you already did: stackoverflow.com/help/mcve - if you only say what you want, maybe a freelance site would be a better place Commented Mar 22, 2018 at 11:42
  • Added the details. Commented Mar 22, 2018 at 12:00
  • Minimal yes but complete and Verifiable not sure. Do you have an actual URL you have been working with? We have no idea where in the return information the column of information you are after is stored. You can look through the many many many questions on SO re extracting information from webscraping and see which closes matches your requirement. Maybe identify the classname or id associated with what you are after and looping over a collection. Commented Mar 22, 2018 at 12:06
  • @QHarr, 161.202.184.168/excelimport.txt Commented Mar 22, 2018 at 12:06

2 Answers 2

1

Such as this to extract?

Windows machines:

add the reference in to microsoft xml and to html object library VBE > tools > references

Also, XMLHTTP60 will need to be adjusted (the 60 bit) to the appropriate version for your Excel.

' "http://161.202.184.168/excelimport.txt"

Sub Getinfo3()

    Dim http As New XMLHTTP60
    Dim html As New HTMLDocument

    With http
        .Open "GET", "http://161.202.184.168/excelimport.txt", False
        .send
        html.body.innerHTML = .responseText
    End With

   Dim returnArray() As String
   returnArray = Split(html.body.innerText, " ")

   Dim currentItem As Long

   For currentItem = LBound(returnArray) To UBound(returnArray)
       ActiveSheet.Cells(currentItem + 1, 1) = returnArray(currentItem)
   Next currentItem

End Sub

And internet explorer version (requires reference to Microsoft Internet Controls and html object library

Public Sub scrapeaIE()

    Dim appIE As Object
    Dim ihtml As Object

    Set appIE = CreateObject("internetexplorer.application")

    With appIE

        .Visible = True
        .navigate "http://161.202.184.168/excelimport.txt"

        While .Busy = True Or .readyState < 4: DoEvents: Wend

        Set ihtml = .document

    End With

    Dim returnArray() As String
    returnArray = Split(ihtml.body.innerText, vbNewLine)

    Dim currentItem As Long

    For currentItem = LBound(returnArray) To UBound(returnArray)
        ActiveSheet.Cells(currentItem + 1, 1) = returnArray(currentItem)
    Next currentItem

    appIE.Quit

    Set appIE = Nothing

End Sub

References included to use both (Excel 2016)

References

  1. Internet Controls
  2. XML Library (version for your excel)
  3. HTML Object library (version for your excel)

Edit:

For Mac:

You may be able to do something with AppleScript and MacScript

Edit:

The OP running the code starting getting cache data returning after the first run with each new URL. In the same way as Excel-VBA REST WCF works on 1st call, but subsequent calls return cached (non-current) data

The resolution was to add:

.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"

Before

.send
Sign up to request clarification or add additional context in comments.

10 Comments

I tried running it but it gives Error 'Compile Error: User-defined type not defined' Attempting to Compile Code as error. I followed this link support.microsoft.com/en-in/help/858611/… , but it just won't compile.
You need to add the reference in to microsoft xml and to html object library VBE > tools > references >
And XMLHTTP60 will be adjusted to the appropriate version number
Am using office 2016 for mac. I don’t have these many options when I go to reference. Am trying to figure out if I am able to download it somehow.
I am writing out data with ActiveSheet.Cells(currentItem + 1, 1) and the last 1 specified the column. So ,2 would be B and ,3 would be C etc e.g. ActiveSheet.Cells(currentItem + 1, 3 is column C.
|
0

Sounds like a job for PowerQuery, depending on your version of Excel it is either built in or is available as an add-in from Microsoft. So for Excel 2016 you just click the Data tab, then New Query > From Other Sources > From Web and then follow the dialog boxes and job done.

You can find out more from here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.