Macro to get the text file from Google search on Chandoo
Some notes in support of these Threads
https://excelfox.com/forum/showthrea...urning-Nothing
https://excelfox.com/forum/showthrea...sult-Using-VBA
Code:
Sub GoogleSearchURL() ' https://excelfox.com/forum/showthread.php/2656-Automated-Search-Results-Returning-Nothing https://excelfox.com/forum/showthread.php/973-Lookup-First-URL-From-Google-Search-Result-Using-VBA
On Error GoTo Bed
'_1 First section get the long text string of the HTML coding of the internet Page
'_1(i) get the long single text string
With CreateObject("msxml2.xmlhttp")
.Open "GET", "https://www.google.com/search?q=Chandoo", False ' 'just preparing the request type, how and what type... "The True/False argument of the HTTP Request is the Asynchronous mode flag. If set False then control is immediately returns to VBA after Send is executed. If set True then control is returned to VBA after the server has sent back a response.
'No extra info here for type GET
.setRequestHeader bstrheader:="Ploppy", bstrvalue:="Poo"
'.setRequestHeader bstrheader:="If-Modified-Since", bstrvalue:="Sat, 1 Jan 2000 00:00:00 GMT" ' https://www.autohotkey.com/boards/viewtopic.php?t=9554 --- It will caching the contents of the URL page. Which means if you request the same URL more than once, you always get the same responseText even the website changes text every time. This line is a workaround : Set cache related headers.
.send ' varBody:= ' No extra info for type GET. .send actually makes the request
While .readyState <> 4: DoEvents: Wend ' Allow other processes to run while the web page loads. Think this is part of the True option
Dim PageSrc As String: Let PageSrc = .responseText ' Save the HTML code in the (Global) variable. ': Range("P1").Value = PageSrc 'For me for a print out copy to text file etc. The responseText property returns the information requested by the Open method as a text string
End With
'_1(ii) Optional secion to put the text string into a text file , for ease of code developments
Dim FileNum2 As Long: Let FileNum2 = FreeFile(0) ' https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/freefile-function
Dim PathAndFileName2 As String
Let PathAndFileName2 = ThisWorkbook.Path & "\" & "Chandoo" & ".txt" ' CHANGE TO SUIT
Open PathAndFileName2 For Output As #FileNum2 ' CHANGE TO SUIT ' Will be made if not there
Print #FileNum2, PageSrc '
Close #FileNum2
End Sub
MID( SUBSTITUTE(Text,Delim,REPT(" ",LEN(Text))) , element*LEN(Text)-(LEN(Text)-1) , LEN(Text) )
Test...
MID( SUBSTITUTE(Text,Delim,REPT(" ",LEN(Text))) , (element*LEN(Text)) - (LEN(Text)-1) , LEN(Text) )
MID( SUBSTITUTE(Text,Delim,REPT(" ",LEN(Text))) , (element*LEN(Text)) - ( LEN(Text) ) , LEN(Text) )
| Row\Col |
A |
B |
C |
1 |
|
|
What is pseudo is in the Cell to the left ( column B ) |
2 |
Example get the first thing, 1 from the Text string "1,3,5" |
1,3,5 |
my original test text |
3 |
Length |
5 |
the length in characters of my original test text |
4 |
( Rept " " ) x Length |
|
5 spaces like "12345" |
5 |
Substitute in the original string ( B2 ) 5 spaces for each comma seperator |
1_____3_____5 |
like "1123453123455" is 13 characters |
6 |
I apply to B5 the MID function starting at (1x5)-(5-1)=1 and for a length of 5 |
1____ |
like "11234" |
7 |
I apply to B5 the MID function starting at (1x5)-(5)=0 and for a length of 5 |
#VALUE! |
Excel doesn't forgive me for trying to start at 0 !!!! |
8 |
|
|
|
9 |
Example get the second thing, 3 from the Text string "1,3,5" |
1,3,5 |
my original test text |
10 |
Length |
5 |
the length in characters of my original test text |
11 |
( Rept " " ) x Length |
|
5 spaces like "12345" |
12 |
Substitute in the original string ( B9 ) 5 spaces for each comma seperator |
1_____3_____5 |
like "1123453123455" is 13 characters |
13 |
I apply to B12 the MID function starting at (2x5)-(5-1)=6 and for a length of 5 |
_3___ |
like "13123" |
14 |
I apply to B12 the MID function starting at (2x5)-(5)=5 and for a length of 5 |
__3__ |
like "12312" |
15 |
|
|
|
16 |
Example get the third thing, 5 from the Text string "1,3,5" |
1,3,5 |
my original test text |
17 |
Length |
5 |
the length in characters of my original test text |
18 |
( Rept " " ) x Length |
|
5 spaces like "12345" |
19 |
Substitute in the original string ( B16 ) 5 spaces for each comma seperator |
1_____3_____5 |
like "1123453123455" is 13 characters |
20 |
I apply to B19 the MID function starting at (3x5)-(5-1)=11 and for a length of 5 |
__5 |
like "125" Note: I try to do length 5, but Excel forgives me and gives the 3 it has available |
21 |
I apply to B19 the MID function starting at (1x5)-(5)=0 and for a length of 5 |
___5 |
like "1235" Note: I try to do length 5, but Excel forgives me and gives the 4 it has available |
| Row\Col |
B |
1 |
|
2 |
1,3,5 |
3 |
=LEN(B2) |
4 |
=REPT(" ",B3) |
5 |
=SUBSTITUTE(B2,",",B4) |
6 |
=MID(B5,(1*B3)-(B3-1),B3) |
7 |
=MID(B5,(1*B3)-(B3),B3) |
8 |
|
9 |
1,3,5 |
10 |
=LEN(B9) |
11 |
=REPT(" ",B10) |
12 |
=SUBSTITUTE(B9,",",B11) |
13 |
=MID(B12,(2*B10)-(B10-1),B10) |
14 |
=MID(B12,(2*B10)-(B10),B10) |
15 |
|
16 |
1,3,5 |
17 |
=LEN(B16) |
18 |
=REPT(" ",B17) |
19 |
=SUBSTITUTE(B16,",",B18) |
20 |
=MID(B19,(3*B17)-(B17-1),B17) |
21 |
=MID(B19,(3*B17)-(B17),B17) |
| Row\Col |
A |
B |
C |
7 |
I apply to B5 the MID function starting at (1x5)-(5)=0 and for a length of 5 |
#VALUE! |
Excel doesn't forgive me for trying to start at 0 when using MID |
MID( SUBSTITUTE(Text,Delim,REPT(" ",LEN(Text))) , element*LEN(Text)-(LEN(Text)-1) , LEN(Text) )
Test...
MID( SUBSTITUTE(Text,Delim,REPT(" ",LEN(Text))) , (element*LEN(Text)) - (LEN(Text)-1) , LEN(Text) )
MID( SUBSTITUTE(Text,Delim,REPT(" ",LEN(Text))) , (element*LEN(Text)) - ( LEN(Text) ) , LEN(Text) )
| Row\Col |
A |
B |
C |
1 |
|
|
What is pseudo is in the Cell to the left ( column B ) |
2 |
Example get the first thing, 1 from the Text string "1,3,5" |
1,3,5 |
my original test text |
3 |
Length |
5 |
the length in characters of my original test text |
4 |
( Rept " " ) x Length |
|
5 spaces like "12345" |
5 |
Substitute in the original string ( B2 ) 5 spaces for each comma seperator |
1_____3_____5 |
like "1123453123455" is 13 characters |
6 |
I apply to B5 the MID function starting at (1x5)-(5-1)=1 and for a length of 5 |
1____ |
like "11234" |
7 |
I apply to B5 the MID function starting at (1x5)-(5)=0 and for a length of 5 |
#VALUE! |
Excel doesn't forgive me for trying to start at 0 !!!! |
8 |
|
|
|
9 |
Example get the second thing, 3 from the Text string "1,3,5" |
1,3,5 |
my original test text |
10 |
Length |
5 |
the length in characters of my original test text |
11 |
( Rept " " ) x Length |
|
5 spaces like "12345" |
12 |
Substitute in the original string ( B9 ) 5 spaces for each comma seperator |
1_____3_____5 |
like "1123453123455" is 13 characters |
13 |
I apply to B12 the MID function starting at (2x5)-(5-1)=6 and for a length of 5 |
_3___ |
like "13123" |
14 |
I apply to B12 the MID function starting at (2x5)-(5)=5 and for a length of 5 |
__3__ |
like "12312" |
15 |
|
|
|
16 |
Example get the third thing, 5 from the Text string "1,3,5" |
1,3,5 |
my original test text |
17 |
Length |
5 |
the length in characters of my original test text |
18 |
( Rept " " ) x Length |
|
5 spaces like "12345" |
19 |
Substitute in the original string ( B16 ) 5 spaces for each comma seperator |
1_____3_____5 |
like "1123453123455" is 13 characters |
20 |
I apply to B19 the MID function starting at (3x5)-(5-1)=11 and for a length of 5 |
__5 |
like "125" Note: I try to do length 5, but Excel forgives me and gives the 3 it has available |
21 |
I apply to B19 the MID function starting at (1x5)-(5)=0 and for a length of 5 |
___5 |
like "1235" Note: I try to do length 5, but Excel forgives me and gives the 4 it has available |
| Row\Col |
B |
1 |
|
2 |
1,3,5 |
3 |
=LEN(B2) |
4 |
=REPT(" ",B3) |
5 |
=SUBSTITUTE(B2,",",B4) |
6 |
=MID(B5,(1*B3)-(B3-1),B3) |
7 |
=MID(B5,(1*B3)-(B3),B3) |
8 |
|
9 |
1,3,5 |
10 |
=LEN(B9) |
11 |
=REPT(" ",B10) |
12 |
=SUBSTITUTE(B9,",",B11) |
13 |
=MID(B12,(2*B10)-(B10-1),B10) |
14 |
=MID(B12,(2*B10)-(B10),B10) |
15 |
|
16 |
1,3,5 |
17 |
=LEN(B16) |
18 |
=REPT(" ",B17) |
19 |
=SUBSTITUTE(B16,",",B18) |
20 |
=MID(B19,(3*B17)-(B17-1),B17) |
21 |
=MID(B19,(3*B17)-(B17),B17) |
| Row\Col |
A |
B |
C |
7 |
I apply to B5 the MID function starting at (1x5)-(5)=0 and for a length of 5 |
#VALUE! |
Excel doesn't forgive me for trying to start at 0 when using MID |