Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

Running a sql function in an Excel macro

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
Josh_Blade
Asked:
Josh_Blade
  • 3
  • 3
1 Solution
 
NorieCommented:
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
 
Josh_BladeAuthor Commented:
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
 
NorieCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Josh_BladeAuthor Commented:
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
 
Josh_BladeAuthor Commented:
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
 
NorieCommented:
Josh

Glad you got it running.

How is it performance wise?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now