Applying a formula to Get From Web URL with VBA Macro - vba

I'm trying to find a way to apply a formula to a get data from web url.
So the url will look like:
http://www.thisurl.com/formula/restOfTheUrl
I'm new to this, so I tried recording the macro using the Advanced Get From Web function and breaking out the elements of the URL from there and substituting the formula in.
So the section of the code looked like this
Source = Xml.Tables(Web.Contents(""http://www.thisurl.com/"" & text(0.123456,2) & ""/restOfTheUrl""))
Had it worked, the number 2 would have been substituted in place of text(0.123456,2).
Is there another way to do this?

Try,
Source = Xml.Tables(Web.Contents("http://www.thisurl.com/" & mid("0.123456", 4, 1) & "/restOfTheUrl"))
'possibly (your sample contradicts your code)
Source = Xml.Tables(Web.Contents("http://www.thisurl.com/" & mid("0.123456", 4, 1) & "restOfTheUrl"))

Related

For-Next loop not stopping

This seems crazy basic to ask, but I can't figure out what I've done wrong. Working in Access VBA to loop through an array, but the FOR loop doesn't stop and I get a "Subscript out of range" error.
compStr = "[" & uniqueIDs(0) & "]=" & rs(uniqueIDs(0))
bnd = UBound(uniqueIDs)
For i = 1 To bnd
compStr = compStr & " and [" & uniqueIDs(i) & "]=" & rs(uniqueIDs(i))
Next i
In this example I'm using the loop to build compStr and bnd stores the array size. uniqueIDs holds 2 records (0-1) and I've confirmed that bnd and UBound(uniqueIDs) value=1. However the FOR statement continues until i=2 and of course I get an error when it executes uniqueIDs(2) on the 4th line. What am I missing? (I've pasted debug window images below)
I did a test but that successfully ran, see below implementation:
Regarding above comments on the data types: I understand why people use Variants in Access: this is because often table values are assigned to variables, and only Variant can pick up NULL. However, in your case something forced the string values to be Strings and not Variants - if I used the line commented out, it would yield Variant/Variant(0 to 1), so I used a typed dummy array - I don't know how otherwise get Variant/String(0 to 1).
Nevertheless, there was no error. Did you compile your code with Debug -> Compile? If not, could you do it and re-try? Sometimes this is necessary, otherwise some code segments newly added/removed are just not executing and the previous compiled version of the code (which is stored by Access) is making a total mess.
If it's still not working, I think you should add the rest of the code as others asked in comments to see how the variables are assigned values, and the version of Access you are using.

VBA Hyperlinks.Add method prepending local folder to Address

I've been lurking here for a while but this is my first post so let me know if I need to change something. Anyways, here goes:
I'm trying to create a macro that will add hyperlinks to cells in a worksheet. The problem is that after running the macro, I notice that the folder location of my spreadsheet has been prepended to the address that I specified. Is there something I need to do in order to indicate that this is a webpage and not a local file? Excerpt from the macro is below.
Dim IGQ As Range
Dim IGQno As String
Dim IGQno1 As String
For Each IGQ In Range("A2:A10") 'Actual range is much larger
IGQno = IGQ.Value
IGQno1 = Left(IGQ, 1)
Sheets("Cameron DCDA").Hyperlinks.Add Anchor:=IGQ, _
Address:="""http://xxxx""&IGQno1&""xxx""&IGQno&""xxxxx""" 'It's a company website so they probably don't want me to share it
Next
The result is that a hyperlink is created for each cell but it links to file:///C:\Users\John.Doe\Documents\"http://xxxx"&IGQno1&"xxx"&IGQno&"xxxxx"
I've tried using fewer quotation marks in the address since it seems like overkill but I get the compile error "Expected: end of statement"
Do you guys have any suggestions?
Too many quotes
Address:="http://xxxx " & IGQno1 & "xxx" & IGQno & "xxxxx"
Also - be sure to leave a space before your & otherwise it will be interpreted as a variable type suffix:
What are possible suffixes after variable name in VBA?

Excel macro / script to cut/paste text between columns

I am working in as a business developer, however the problems in our support department sometimes require me to work on clients' CSV file i.e. Excel spreadsheet containing product catalog.
Right now I have an Export CSV from an online store, where the first column is formatted in the following way:
Name_of_the_product - {store category)
e.g. Sony NEX-6 - {digital camera}
I need a macro/script to take cut the words between {} and paste it into the column immediately to the right, without the {}. This way I will have clean product name i.e. Sony NEX-6, as well as the store category for each product e.g. digital camera (which I need to push the product feed via a third party solution).
Any help would be greatly appreciated!
I have managed to get it mostly working using the following function:
=MID(A2;FIND("[";A2)+1;FIND("]";A2) - FIND("[";A2) - 1)
The only thing now left to do is to delete what was copied from the A column (including the []) - does anyone have the idea for the macro? I hope I can get it working using a Search/Replace functionality from a feed management solution, but I am checking with our support for that.
Thank you all!
I assume your data in column A. And have 100 rows.
Dim WrdArray() As String
Dim text_string As String
Dim LeftSide As String
Dim RightSide As String
For i = 1 To 100
text_string = Range("A" & i).Value
WrdArray() = Split(text_string, "{")
LeftSide = WrdArray(LBound(WrdArray))
RightSide = WrdArray(UBound(WrdArray))
Range("A" & i).Value = LeftSide
Range("B" & i).Value = Replace(Result, "}", "")
Next i
And if you want to understand the split function, you can refer this blog post.
http://www.excelvbasolutions.com/2015/06/split-function.html

Excel VBA - Accessing transit_details array from Google directions API using XML

I am accessing the Google Directions API from Excel using VBA. I would like to be able to access the transit_details array in the Google directions API when retrieving transit directions using XML from VBA so I can get number of stops etc. At the moment I have code that gives me basic directions but I don't get stop numbers etc for transit directions:
The following is the Google Directions API webpage: https://developers.google.com/maps/documentation/directions/
'Get Directions
For Each nodeRoute In .SelectSingleNode("//route/leg").ChildNodes
If nodeRoute.BaseName = "step" Then
strInstructions = strInstructions & nodeRoute.SelectSingleNode("html_instructions").text & " - " & nodeRoute.SelectSingleNode("distance/text").text & vbCrLf
End If
Next
strInstructions = CleanHTML(strInstructions) 'Removes MetaTag information from HTML result to convert to plain text.
Else
strError = .SelectSingleNode("//status").text
GoTo errorHandler
End If
End With
Sounds like you need an XML Parser for VBA so that you can reference the XML with it's native structure. I'm not sure if there's XML parsers for VBA, I'll look when I get home from work (no time now)

VB scripting Add hyperlinks

I want to add several hyperlinks in a word document using VBscript.
but I am able to add the hyperlinks at the same place everytime overwriting the previous one.
Code Provided by Author
For TRow = 2 To Target_LastRow
ObjWord.ActiveDocument.Hyperlinks.Add
Anchor:=SAnchr, _ Address:=getExcelObj.Worksheets(2).Cells(TRow, 4).Value, _
TextToDisplay:="Link" & (TRow - 1)
This is what I could find by the code provided by you. I hope this is a start.
ObjWord.ActiveDocument.Hyperlinks.Add(Anchor:=Ank, Address:=M, SubAddress:=SA, ScreenTip:=M, TextToDisplay:=M, Target:=M)
You can go a step simpler and just place it like so Range would be "A1-B2" Or something similar
range.Hyperlinks.Add(range, "http://www.microsoft.com")
I also would recomend you follow This Link
Sources 1,2
Update
To add to your overwriting, you must change the value that you chose to represent the range or area of insertion. A wild hunch of mine tells me that it would be the anchor Variable