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.
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 . - Option Explicit
-
Const Cd As Long = 2 ' "Date" column (B)
-
Const Cr As Long = 4 ' "Returns" column (D)
-
' Const Cr As Long = Cd + 2 'This is declared relative to Cd
-
Const Ca As Long = 5 ' "Adjust" column (E)
-
' Const Ca As Long = Cd + 3 'This is declared relative to Cd
-
' Const Ca As Long = Cr + 1 'This is declared relative to Cr
-
Const FirstRow As Long = 5 ' First row with data in your table
-
-
Dim Msg As VbMsgBoxResult 'Working variable
-
-
Private Sub cmdStartAdjust_Click()
-
-
Dim Rd As Long 'Row in "Date" column
-
Dim Rr As Long 'Row in "Returns" column
-
Dim Ra As Long 'Row in "Adjusted" column
-
-
Dim FirstPercent As Double, SecondPercent As Double, AdjustedPercent As Double
-
Dim nDays As Long 'Number of days for adjustement
-
-
Rd = FirstRow
-
Do
-
FirstPercent = Cells(Rd, Cr)
-
'Find the SecondPercent and number of days
-
nDays = 1
-
Rr = Rd + 1
-
Do Until Not IsEmpty(Cells(Rr, Cr))
-
Rr = Rr + 1
-
nDays = nDays + 1
-
If IsEmpty(Cells(Rr, Cd)) Then
-
'The last date has not a "Returns" value
-
Msg = MsgBox("No end for ""Returns""", vbCritical)
-
Exit Sub
-
End If
-
Loop
-
SecondPercent = Cells(Rr, Cr)
-
'Calculate AdjustedPercent
-
AdjustedPercent = (SecondPercent - FirstPercent) / nDays
-
'Write AdjustedPercent in "Adjusted" column
-
For Ra = Rd + 1 To Rr
-
Cells(Ra, Ca).Select
-
Selection.NumberFormat = "0.0000%" 'Format cell as percent
-
Cells(Ra, Ca) = AdjustedPercent
-
Next Ra
-
Rd = Rr
-
Loop Until IsEmpty(Cells(Rd + 1, Cd)) 'No more days
-
End Sub
4 1757
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 . - Option Explicit
-
Const Cd As Long = 2 ' "Date" column (B)
-
Const Cr As Long = 4 ' "Returns" column (D)
-
' Const Cr As Long = Cd + 2 'This is declared relative to Cd
-
Const Ca As Long = 5 ' "Adjust" column (E)
-
' Const Ca As Long = Cd + 3 'This is declared relative to Cd
-
' Const Ca As Long = Cr + 1 'This is declared relative to Cr
-
Const FirstRow As Long = 5 ' First row with data in your table
-
-
Dim Msg As VbMsgBoxResult 'Working variable
-
-
Private Sub cmdStartAdjust_Click()
-
-
Dim Rd As Long 'Row in "Date" column
-
Dim Rr As Long 'Row in "Returns" column
-
Dim Ra As Long 'Row in "Adjusted" column
-
-
Dim FirstPercent As Double, SecondPercent As Double, AdjustedPercent As Double
-
Dim nDays As Long 'Number of days for adjustement
-
-
Rd = FirstRow
-
Do
-
FirstPercent = Cells(Rd, Cr)
-
'Find the SecondPercent and number of days
-
nDays = 1
-
Rr = Rd + 1
-
Do Until Not IsEmpty(Cells(Rr, Cr))
-
Rr = Rr + 1
-
nDays = nDays + 1
-
If IsEmpty(Cells(Rr, Cd)) Then
-
'The last date has not a "Returns" value
-
Msg = MsgBox("No end for ""Returns""", vbCritical)
-
Exit Sub
-
End If
-
Loop
-
SecondPercent = Cells(Rr, Cr)
-
'Calculate AdjustedPercent
-
AdjustedPercent = (SecondPercent - FirstPercent) / nDays
-
'Write AdjustedPercent in "Adjusted" column
-
For Ra = Rd + 1 To Rr
-
Cells(Ra, Ca).Select
-
Selection.NumberFormat = "0.0000%" 'Format cell as percent
-
Cells(Ra, Ca) = AdjustedPercent
-
Next Ra
-
Rd = Rr
-
Loop Until IsEmpty(Cells(Rd + 1, Cd)) 'No more days
-
End Sub
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.
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.
Mihail: A few adjustments here and there from my end but the code worked perfectly. Cheers.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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 '<'...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |