I have an SQL query string. In this string are multiple different columns of data, all coming from the same table. For simplicities sake, here is a short example: SELECT 'A'.'B', 'A'.'C', 'A'.'D'
Where 'A' is the alias name. My question is: Is there a way using VBA to take that query string, and remove all instances of the alias 'A' and just return the data columns 'B', 'C', 'D'?
Please no full code, I like starting points. I like to fill in the pieces on my own. It helps the learning process.
2 Answers
- Recover the SQL under the QueryTable like this:
Recover SQL:
Range("A1").Select 'Your query worksheet
With Selection.QueryTable
queryText = .CommandText
End With
Then replace the aliases with
LEFT,RIGHT,INSTRorRegExpobject (if you want to learn something useful and quick useRegExp- regular expressions). If you need an example regex let me know in the comments. If you want to learn Regex in VBA I made a tutorial here.Replace the query and refresh the QueryTable
Replace and refresh query
Range("A1").Select 'Your query worksheet
With Selection.QueryTable
.CommandText = queryText
.Refresh BackgroundQuery:=False
End With
1 Comment
Constuntine
Thank you. I do believe this is what I needed.
'B','C','D'fields for other purposes. I apologize for the ambiguity. (still kind of new to this site)