Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Running a sql function in an Excel macro

Posted on 2012-03-12
6
Medium Priority
?
139 Views
Last Modified: 2012-03-13
I have a scalar function I have written in SQL. It takes in 2 date times and spits out our business cycle time.

I'd like to be able to have a macro that will look at two columns of cells and run the function for each row that has data.

IE, I want columns A and B to have a bunch of rows with Date1 and Date2.
I want to run a macro that will put
myDateFunction(A1, B1) into column C1
myDateFunction(A2, B2) into column C2

etc...
0
Comment
Question by:Josh_Blade
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 34

Expert Comment

by:Norie
ID: 37711345
If all you want to do is put that function in column C for each row with data try this.
LastRow =Range("A"& Rows.Count).End(xlUp).Row

Range("C1:C" & C).Formula = "=myDateFunction(A1,B1)"

Open in new window


If it's more complicated than that please post further details.
0
 

Author Comment

by:Josh_Blade
ID: 37711509
imnorie,
 Thanks for the quick response.
I entered the below into the macro
LastRow = Range("A" & Rows.Count).End(xlUp).Row

Range("C1:C" & C).Formula = "=dbo.CycleTimeASIhmmss(A1,B1)"

Open in new window


When I run it, I get a

"Run-time error '1004':
Method 'Range' of object '_Global' failed." error message.
(in debug, it highlights the second line as causing the problem)


Here's the Sample data I used:
     A                B                C
01/13/12      02/01/12
01/27/12      02/03/12
01/16/12      02/08/12
01/16/12      02/08/12


Looking around the web a little bit, I just wanted to be clear that I'm wanting to use a scalar function from SQL. I've noticed a lot of other posts about this topic creating connection strings (this spreadsheet does already have a connection string set up in the data tab if that makes a difference).
0
 
LVL 34

Accepted Solution

by:
Norie earned 2000 total points
ID: 37711824
Sorry, my mistake - I was in a rush.

It should read like this.
LastRow = Range("A" & Rows.Count).End(xlUp).Row

Range("C1:C" & LastRow).Formula = "=dbo.CycleTimeASIhmmss(A1,B1)"

Open in new window


Anyway, that doesn't really matter now you've explained what you actually have.

What I posted was for an Excel UDF written in VBA but I did think there was more to it.

If what you actually have is a scalar function in SQL then you need another approach.

One way would be to create a UDF that connects to the database, runs the function using the cell values as parameters and returns the value.

The possible problem with that is that you would have to make the connection every time the UDF was used, so if you had the formula in C1:C100 that would be 100 separate connections.

Also, UDFs are usually calculated whenever a worksheet is calculated, so that would be 100 connections every time.

How often would you need to calculate the function?

Is it a one-off or will the values in the columns be changed or new values added?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Josh_Blade
ID: 37714409
imnorie,
 Thanks again for your informed response. After some more digging around, i've stumbled across a few similar UDFs that other people have written. I've tweaked it to what I think should work for me, but I keep getting

Comile error:
User-defined type not defined

and the debugger highlights the signature of my function.

Public Function CycleTime(FromDate As Date, ToDate As Date) As String
     
   Dim cn As New ADODB.Connection, cmd As New ADODB.Command, rs As ADODB.Recordset
      cn.Open "Provider=SQLOLEDB;Data Source=MYSERVER;Initial Catalog=MYCATALOG;Integrated Security=SSPI;"
      cmd.ActiveConnection = cn
      cmd.CommandText = "Select dbo.CycleTimeASIhmmss('" & FromDate & "','" & ToDate & "')"
      
      Set rs = cmd.Execute()
      rs.MoveFirst
      CycleTime = rs(0)
End Function

Open in new window


From what I'm reading there's some kind of reference missing (probably something to do with the ADO/sql stuff that's going on). I was hoping you might have an idea of what that would be. If not, thanks for all of your help!

Edit: After some testing, I was able to verify that it was this line causing the problem, so I need to find the right reference for it. I'll update when I find it

 
Dim cn As New ADODB.Connection, cmd As New ADODB.Command, rs As ADODB.Recordset

Open in new window

0
 

Author Closing Comment

by:Josh_Blade
ID: 37715051
I needed the Microsoft ActiveX Data Objects 2.0 Library
You can find it by going to Tools>References from the vba window.

Here's a link in another EE page I found:
link
0
 
LVL 34

Expert Comment

by:Norie
ID: 37715215
Josh

Glad you got it running.

How is it performance wise?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question