VBA script for Companies House - not working anymore? Thanks for checking

boxhamster

New Member
Joined
Jan 12, 2016
Messages
14
I had this script made by a mate once, but now something is not working anymore. It's says "done", but actually cells were not populated with values. What it does it takes a company name in cell A and then populates cells B-E with CRN SIC Postcode Addresss.
I'd be very grateful for some help. From what I see, it enters the company and searches for it, but then when trying to go to /company/CRN number the pages doesn't work. Not sure how to fix this?
Thanks for looking and helping.

Code:
Sub companieshouse()
Dim lastrow As Long
Dim whblock() As String
Dim siccon As Integer
Dim remspace As String
Dim final As String
Dim ie As New InternetExplorer
siccon = 0
lastrow = ActiveSheet.Range("A:A").End(xlDown).Row  

  ie.Visible = True
For I = 2 To lastrow
remspace = Replace(ActiveSheet.Range("A" & I).Value, " ", "+")
final = Replace(remspace, "&", "%26")  
  ie.navigate "[URL]https://beta.companieshouse.gov.uk/search/companies?q=[/URL]" & final
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As New HTMLDocument
Set doc = ie.document
Dim name, name2 As String
name = doc.getElementsByTagName("li")(4).innerText  
  If Not doc.getElementsByTagName("li")(4) Is Nothing Then
whblock() = Split(name, vbCrLf)  
  If InStr(1, whblock(0), UCase(ActiveSheet.Range("A" & I).Value)) > 0 Then
ActiveSheet.Range("B" & I).Value = Trim(Mid(whblock(1), 1, InStr(1, whblock(1), "-") - 1))
ActiveSheet.Range("D" & I).Value = Mid(whblock(2), InStrRev(whblock(2), ",") + 1)
ActiveSheet.Range("E" & I).Value = Mid(whblock(2), 1, InStrRev(whblock(2), ",") - 1)  
  End If  
  [B]ie.navigate "[URL]https://beta.companieshouse.gov.uk/company/[/URL]" & ActiveSheet.Range("B" & I).Value[/B]
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE  
  Set doc = ie.document  
  Do Until doc.getElementById("sic" & siccon) Is Nothing
name2 = name2 & vbCrLf & doc.getElementById("sic" & siccon).innerText
siccon = siccon + 1
Loop  
  ActiveSheet.Range("C" & I).Value = Replace(name2, vbCrLf, "", 1, 1)  
  name = ""
name2 = ""
Erase whblock()
siccon = 0  
  End If  
  Next
ie.Quit
MsgBox "Done"
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I don't have your access. Does this help?
Code:
'ie.navigate "https://beta.companieshouse.gov.uk/search/companies?q=" & final
ie.navigate "https://beta.companieshouse.gov.uk/company/" & final
 
Upvote 0
It is probably in the parsing routine. Can you detail one example and the expected results? Post and manually marked up one to a site like dropbox.com if you like. Screen pics of a manual navigation might help too.

I looked at Trade Limited, col A value of 02693994, in my test and had no parsed results returned either.
 
Last edited:
Upvote 0
Upvote 0
Slightly odd code - initialising variables after they have been used!

Use the API and then you don't have to worry about the webpage changing.

For a quick fix, try changing:
Code:
name = doc.getElementsByTagName("li")(4).innerText  
  If Not doc.getElementsByTagName("li")(4) Is Nothing Then
to:
Code:
        If Not doc.getElementsByTagName("li")(5) Is Nothing Then
            name = doc.getElementsByTagName("li")(5).innerText
other changes may be needed.
 
Upvote 0

Forum statistics

Threads
1,214,608
Messages
6,120,500
Members
448,968
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top