Hello,
Unfortunately, I am not a VBA guy. I researched that option and VBA code
would require static cell references (???) which would not work for me.
Essentially, I want to be able to copy this "formula" to anyplace in the
worksheet and have it pull info from SQL based upon the value of the cell to
the left of it.
> Can you use VBA?
>
[quoted text clipped - 15 lines]
> >
> > Anyone up to the challenge? Thanks in advance! :)
Ross Culver - 28 Nov 2007 22:03 GMT
You could, of course, still do this in VBA. In fact, I'm not sure you have
any other choice since there's no 'Get External Data' approach for multiple
parameters as you've described. Below is a sample of what the vba code
might look like.
Dim param 'This will be used for your column A value
Dim str As String, cnn As New ADODB.Connection, X As Integer, cnnStr As
String
cnnStr = "Data Source=SQLDatabaseServer;Initial Catalog=SQLDatabase;Persist
Security Info=True;User
ID=sa;Password=whatever;MultipleActiveResultSets=True;Connect Timeout=180"
X = 2 ' Assuming your first A value is in row 2
cnn.ConnectionString = cnnStr
cnn.Open
Do while Sheets("Sheet1").range("A" & x).value <> ""
Dim rst As New ADODB.Recordset
rst.ActiveConnection = cnn
rst.CursorLocation = adUseClient
param = Sheets("Sheet1").range("A" & X).value
str = "Select * from tableA where fieldA = '" & param & "'"
rst.Open str
If Not rst.EOF Then
rst.MoveFirst
Do While Not rst.EOF
Sheets("Sheet1").Range("B" & X).Value = rst.Fields.Item(1).Value
'rst.MoveNext ' Watch this! If you know there's only one value
that can be returned from SQL then there's no need to loop through the
recordset. If you want to return multiple values,
' then use another variable to set the row
to write to.
Loop
End If
rst.Close
x = x + 1
loop
cnn.Close
Make sense?
Ross
> Hello,
>
[quoted text clipped - 28 lines]
>> >
>> > Anyone up to the challenge? Thanks in advance! :)
cpa-mcse.net - 29 Nov 2007 16:41 GMT
Thanks for the code. I will try it out. I also received this VBA solution:
http://www.mrexcel.com/board2/viewtopic.php?p=1459805#1459805
I'm not a VB guy, but hopefully I will get either (or both) to work.