Question: I have an Excel spreadsheet that contains the winning numbers for a lottery. I'd like to count the number of times a pair of numbers occurs in the various draws. How can I do this? Answer: This can be done using VBA code to generate the matched pair counts and then the VLOOKUP function to move the results into a matrix. Let's take a look at an example. Download Excel spreadsheet (as demonstrated below)
|
In this example spreadsheet, we have a sheet called "Draw Data" that contains the winning numbers for several draws. On this sheet is a button called "Update Pair Stats" that calls a macro called UpdatePairStats. When the macro runs, it will populate a sheet called "PairStats" with the matched pair counts as follows:
|
When the macro has completed, the following message box will appear:
|
On the sheet called Pairs, you will see that the matrix has been filled in with the matched pair counts (based on a VLOOKUP formula).
|
The matrix uses the following formula (cell C2 contains the following formula): =IF(ISNA(VLOOKUP(Pairs!$A2 & "." &Pairs!C$1,PairStats!$A:$D,4,FALSE)),"",VLOOKUP(Pairs!$A2 & "." &Pairs!C$1,PairStats!$A:$D,4,FALSE)) What this formula does is perform a VLOOKUP for the concatenated numbers (separated with a "."). If no match is found, it returns an empty string (""). You can press Alt-F11 to view the VBA code. Macro Code:The macro code looks like this: (and is found in Module1)
Please note that you will have to customize the LRange variable to match the number of rows and columns for your data. |
Visit our busy book shop, were you can find latest books for beginners as well as advanced excel users.
Spreadsheet 123 Spreadsheet Software Development
See Spreadsheets for:
a.Hotel Management b.Personal Budget
c.Corporate Finance d.Rating Calculation e.Real Estate and Mortgage
f.Web Calc