4
votes

I have an Excel spreadsheet which I use to calculate the cost of a product we sell, (happens to be car insurance). This product is calculated based on various pieces of information; the customers age, postcode, vehicle, occupation, etc etc.

I enter information onto the main tab of my spreadsheet and then perform lots of vlookup formulas to return the relevant figures from other tabs of the spreadsheet. An example of one of my vlookups is:

=VLOOKUP(G1,RatesInsurerX!A36:B986786,2,FALSE)

So this looks up the value I enter into cell G1 an matches it to the data found on the workbook tab called RatesInsurerX.

As you can see this particular table contains nearly 1 million rows and excel is starting to struggle so I was wondering if there is a way of doing exactly the same thing only performing the lookup against a SQL table rather than the RatesInsurerX workbook?

2
Do you have the data in an SQL table already? - bendataclear
For this particular lookup - Yes. Most of my vlookups work perfectly well in Excel but the postcodes tables for the product has up-to and over 1 million rows and we have multiple insurers too so I'm just looking to reduce the size and complexity of the spreadsheet by calling external data. I just need a nudge in the right direction. - RumDemon
For your current lookup are you entering the formula once for the sheet or copying down into a table? ie do you need to look up multiple values as part of a table? - bendataclear
Just enter it once and calculate the premium. If I then need to calculate another premium on a different risk then I enter the new postcode in place of the old one. Hope that make sense. - RumDemon

2 Answers

8
votes

Good news is you can do this without VBA, quite a few steps though as follows:

1 . First add a new sheet, call this something meaningful like PoscodeLookup.

2 . Next go to Data and select Other Sources and Microsoft Query:

Microsoft Query

3 . Next select (or create) an ODBC data source that can connect you to your database asd select this (you may need to enter user/pass).

4 . The query designer will ask you to select a table, just click close.

Click Close

5 . Next select the SQL button:

SQL button

6 . Now enter the SQL query to get the single value you need, using an example postcode eg:

SELECT TOP 1 [Value] FROM [MyTable] WHERE [Postcode] = 'AB12 1AA';

7 . Now hit OK and OK which should return a single value in the window.

8 . Click on Return data in the toolbar:

Return data

9 . Now back in Excel hit properties in the prompt:

Properties prompt

10 . Now change the post code you entered into a ? in the definition tab:

Edit Query

11 . Hit OK and OK again and it will prompt for a value, enter a valid postcode and hit enter, this should now give you the value you want in cell A1.

12 . Now click in the cell A1 and go to Data > Properties then Connection properties:

Properties

Connection properties

13 . Now in the definition tab you have a Parameters button at the bottom, in here you can fill out as below:

Parameters

Note, the target cell is the cell where you enter the postcode, tick the refresh box if you want this to re-run the query when the post code changes.

That should be it, let me know if it doesn't work.

0
votes

You can. In the VBA of the sheet, create a function like so:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

and in it's body

  • Check the Target is the cell you want to base your lookup on (G1)
  • If it is, get the data from the DB and put it where it needs to go

Cheers -