473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Change Excel Cell Values using a Function

2
I'm looking to load data in an excel sheet from a database. I would like to do this via a function, so that when I recalc the sheet, the data is loaded. The function is something like:

=loaddata(product, startdate as date, enddate as date) in cell A1. Data for the product between startdate and enda date to be loaded in cells A2 and following.
I do not want to call the data from an array which could directly read the return array from loaddata() as I want this function to execute only if the date changes.

I have seen this done. Bloomberg, a financial news and data provider has functions that return data to cells other than the one that calls the function.

Can you help?
Jul 28 '09 #1
2 4032
MikeTheBike
639 Expert 512MB
@harius
Hi

This is the way I would tackle it
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Sub Worksheet_Change(ByVal Target As Range)
  4.     If Target.Address = Range("StartDate").Address Or Target.Address = Range("EndDate").Address Then
  5.         If IsDate(Target) Then LoadData Sheets("Sheet2").Range("A2")
  6.     End If
  7. End Sub
  8.  
  9. Sub LoadData(ByVal DataRange As Range)
  10.     Dim cn  As New ADODB.Connection
  11.     Dim rs As New ADODB.Recordset
  12.     Dim sql As String
  13.  
  14.     cn.Open "file name=H:\My Data Sources\CES Information DB DEV.udl"
  15.  
  16.     sql = "SELECT * FROM tblEmployees WHERE StartDate " & _
  17.             "BETWEEN #" & Format(Range("StartDate"), "mm/dd/yy") & "# " & _
  18.             "AND #" & Format(Range("EndDate"), "mm/dd/yy") & "#"
  19.  
  20.     rs.Open sql, cn, adOpenStatic, adLockReadOnly
  21.  
  22.     DataRange.CopyFromRecordset rs
  23.  
  24.     rs.Close
  25.     cn.Close
  26.  
  27.     Set rs = Nothing
  28.     Set cn = Nothing
  29.  
  30. End Sub

As you can see this uses the worksheet change event. I have also defined the startdate and enddate cells with a range name. The LoadData sub only fires if one of the date cells is changed (and changed to a date!).

You can then copy the data to any sheet in the wokbook if you want (I used Sheet 2 as it would have overwritten my dates otherwise).

Hope this is of some help.

If it is and you have any queries please ask.


MTB

ps. I have used the for function because I am in the UK but US date formats are required for sql strings!!
Jul 28 '09 #2
harius
2
Thanks for your post.

I understand it addresses the problem I posted. However, it is only part of my problem.
Here is the full problem:

I have a series of products I need to load data for. I have the product Ids for these in a row, say B1, C1, etc... K1.
A1 has today date (end date of the data series) and A2 the start date, say 1/1/2009.
When today() changes, I would like all the data series to recompute on recalc.

I am currently using a loaddata() function taking B2:B10000 as input, with the drawbacks I listed in my first post.

I could of course play with your solution and make it work. However a function based solution would be more nimble. I am specifically looking for such a solution as I have seen it done. Bloomberg has a function for historical data series of securities that is entered on a single cell in the form =BDP(ticker, fields, startdate, enddate) and that returns an array of nbfields columns and nbdates rows. No idea how it's done though...


Regards,

Hari
Jul 28 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
2
by: Bertrand | last post by:
Hello, I am trying to archieve the following: - copy an excel file present on the server - insert values into named ranges of the copy I am using Excel97 on my PC, the server does not have...
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
4
by: Jerry | last post by:
I am new to C# and have been trying to figure out how to access a range passed to C#. I have tried everything I can find and have been unable to get it to work. Here is a test sample Ive been...
6
by: Kevin Humphreys | last post by:
Hi There, I am trying to export a recordset to an excel file using the Content Type below in the header. Response.ContentType = "application/vnd.ms-excel" Which works fine however the...
5
by: Elena | last post by:
I need the VB.NET code to change the header/footer of an Excel spread sheet, ASAP. I am doing it through a VB application. I can change the ranges/cell values using code, but I do not know how to...
18
by: Frank M. Walter | last post by:
Hello, I have made an small AddIn with udf for excel 2003. I use vs2003. The point of view is the function __T() I call it in excel sheet writing =__T() I am not able to set a value to a...
2
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.