473,385 Members | 1,449 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.

Interpolating data between two occurrences to work out individual date values

5
Hi,

I’ve got a googly on my end.

I’m aiming to create a function or procedure that would take the difference between ‘a’ and ‘b' (i.e. ‘x’) where ‘a’ and ‘b’ are spaced ‘y’ cells apart (‘y’ could be 4,5,6 etc.), dividing ‘x’ by ‘y’ to get ‘z’, and then inputting ‘z’ in the previous ‘y’ cells plus in into cell ‘b’.

Hope this makes sense but if not, I have attached a sample workbook.

As always – assistance is much appreciated.

Thanks.
Attached Files
File Type: xlsx Test_Interpolation.xlsx (18.2 KB, 333 views)
Apr 2 '12 #1

✓ answered by Mihail

This code solve your problem.
Note that the code will not work if you change the data position in sheet.
If you do that you must change the constants values.

In attached workbooks (.xls and .xlsm) go to Sheet2 and press Adjust button to see the results.
The code is also in Sheet2 .

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Const Cd As Long = 2 ' "Date" column (B)
  3. Const Cr As Long = 4 ' "Returns" column (D)
  4. '    Const Cr As Long = Cd + 2 'This is declared relative to Cd
  5. Const Ca As Long = 5 ' "Adjust" column (E)
  6. '    Const Ca As Long = Cd + 3 'This is declared relative to Cd
  7. '    Const Ca As Long = Cr + 1 'This is declared relative to Cr
  8. Const FirstRow As Long = 5 ' First row with data in your table
  9.  
  10. Dim Msg As VbMsgBoxResult 'Working variable
  11.  
  12. Private Sub cmdStartAdjust_Click()
  13.  
  14. Dim Rd As Long 'Row in "Date" column
  15. Dim Rr As Long 'Row in "Returns" column
  16. Dim Ra As Long 'Row in "Adjusted" column
  17.  
  18. Dim FirstPercent As Double, SecondPercent As Double, AdjustedPercent As Double
  19. Dim nDays As Long 'Number of days for adjustement
  20.  
  21.     Rd = FirstRow
  22.     Do
  23.         FirstPercent = Cells(Rd, Cr)
  24.         'Find the SecondPercent and number of days
  25.         nDays = 1
  26.         Rr = Rd + 1
  27.         Do Until Not IsEmpty(Cells(Rr, Cr))
  28.             Rr = Rr + 1
  29.             nDays = nDays + 1
  30.             If IsEmpty(Cells(Rr, Cd)) Then
  31.                 'The last date has not a "Returns" value
  32.                 Msg = MsgBox("No end for ""Returns""", vbCritical)
  33. Exit Sub
  34.             End If
  35.         Loop
  36.         SecondPercent = Cells(Rr, Cr)
  37.         'Calculate AdjustedPercent
  38.         AdjustedPercent = (SecondPercent - FirstPercent) / nDays
  39.         'Write AdjustedPercent in "Adjusted" column
  40.         For Ra = Rd + 1 To Rr
  41.             Cells(Ra, Ca).Select
  42.             Selection.NumberFormat = "0.0000%" 'Format cell as percent
  43.             Cells(Ra, Ca) = AdjustedPercent
  44.         Next Ra
  45.         Rd = Rr
  46.     Loop Until IsEmpty(Cells(Rd + 1, Cd)) 'No more days
  47. End Sub

4 1757
Mihail
759 512MB
This code solve your problem.
Note that the code will not work if you change the data position in sheet.
If you do that you must change the constants values.

In attached workbooks (.xls and .xlsm) go to Sheet2 and press Adjust button to see the results.
The code is also in Sheet2 .

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Const Cd As Long = 2 ' "Date" column (B)
  3. Const Cr As Long = 4 ' "Returns" column (D)
  4. '    Const Cr As Long = Cd + 2 'This is declared relative to Cd
  5. Const Ca As Long = 5 ' "Adjust" column (E)
  6. '    Const Ca As Long = Cd + 3 'This is declared relative to Cd
  7. '    Const Ca As Long = Cr + 1 'This is declared relative to Cr
  8. Const FirstRow As Long = 5 ' First row with data in your table
  9.  
  10. Dim Msg As VbMsgBoxResult 'Working variable
  11.  
  12. Private Sub cmdStartAdjust_Click()
  13.  
  14. Dim Rd As Long 'Row in "Date" column
  15. Dim Rr As Long 'Row in "Returns" column
  16. Dim Ra As Long 'Row in "Adjusted" column
  17.  
  18. Dim FirstPercent As Double, SecondPercent As Double, AdjustedPercent As Double
  19. Dim nDays As Long 'Number of days for adjustement
  20.  
  21.     Rd = FirstRow
  22.     Do
  23.         FirstPercent = Cells(Rd, Cr)
  24.         'Find the SecondPercent and number of days
  25.         nDays = 1
  26.         Rr = Rd + 1
  27.         Do Until Not IsEmpty(Cells(Rr, Cr))
  28.             Rr = Rr + 1
  29.             nDays = nDays + 1
  30.             If IsEmpty(Cells(Rr, Cd)) Then
  31.                 'The last date has not a "Returns" value
  32.                 Msg = MsgBox("No end for ""Returns""", vbCritical)
  33. Exit Sub
  34.             End If
  35.         Loop
  36.         SecondPercent = Cells(Rr, Cr)
  37.         'Calculate AdjustedPercent
  38.         AdjustedPercent = (SecondPercent - FirstPercent) / nDays
  39.         'Write AdjustedPercent in "Adjusted" column
  40.         For Ra = Rd + 1 To Rr
  41.             Cells(Ra, Ca).Select
  42.             Selection.NumberFormat = "0.0000%" 'Format cell as percent
  43.             Cells(Ra, Ca) = AdjustedPercent
  44.         Next Ra
  45.         Rd = Rr
  46.     Loop Until IsEmpty(Cells(Rd + 1, Cd)) 'No more days
  47. End Sub
Attached Files
File Type: zip Test_Interpolation.zip (44.8 KB, 45 views)
Apr 2 '12 #2
NeoPa
32,556 Expert Mod 16PB
Chanko,
Please consider posting your example data in the thread itself, rather than as an attachment. Attachments cause much more overhead for a reader of your thread than being able to read the question on the page. Many readers will not bother to go to the extra effort, especially if they can't clearly and easily see what your question is.
Apr 2 '12 #3
Chanko
5
Mihail: Much appreciated - I'm going to work on this and will advise if I need further assistance.
NeoPa: Understand - thanks for letting me know.

These excel forums are amazing! Thanks again.
Apr 2 '12 #4
Chanko
5
Mihail: A few adjustments here and there from my end but the code worked perfectly. Cheers.
Apr 3 '12 #5

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

Similar topics

16
by: Tim Davidge | last post by:
Hi folks, been a while since I have posted a plea for help and I think I have forgotten everything I learnt from the helpful contributors to this newsgroup, that said however : I'm trying to...
2
by: Keith | last post by:
Good Afternoon, New to .Net. I am trying to pass date/time values to a MS Access query depending on what value is selected from a dropdown list box (January, February, etc). I have declared...
6
by: Dean Slindee | last post by:
I am looking for the "right" way to handle inserting and presenting null date values. Public Const c_NullDate As Date = #12:00:00 AM# If I set the value of a date variable in an SQL Server insert...
4
by: Bruno | last post by:
How do I check if a date variable is empty? I mean, if I declare a variable like this dim d as Date how do I know if any date was assigned to d? example, d = now
5
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
2
by: Micela | last post by:
Hi, Please help me out in this task. I have a filed in the database of datetime. and 3 dropdown lists in the page. Im using Asp.net with VB coding. The three dropdown lists are 1.for date...
3
by: a | last post by:
In the example below, I'm trying to simply find all the date values in an XML document (the XML is a string in this example) and then add an upper case Z between the last digit and the closing '<'...
1
by: ghjk | last post by:
I'm developing web site using php and mysql. In there user has to select a date, and data belongs to that date should print.My date field type is datetime. This is my code and it is not...
13
by: chromis | last post by:
Hi, I have a query which updates the projects table of my database, however when I try to run my query with blank values i get the following error: Data truncation: Data truncated for column...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.