Find exact match, and the closest smallest and highest numbers.

Sunline

Well-known Member
Joined
Oct 6, 2007
Messages
701
Office Version
  1. 2016
Platform
  1. Windows
Hello all,
Firstly, I have to inform this forum that this is a cross post thread, my reason is that someone did answer my thread, but I was unable to obtain a final result due to my
lack of vba knowledge. I am wanting to follow all rules and comply safely. Advice was taken from a forum moderator.
[Find exact match, and the closest smallest and highest numbers.]
In this sheet are two macros in modules 1 and 2.
These were written by another member and not me. A lot of effort went into this and I'm just trying to get the code to work. Unfortunately, the person never replied back.
I don’t know how or if they are supposed to join together in some way, and I didn’t understand which part I was supposed to replace.
So, the following is my first original question but now on this site.
I am looking to get a macro for the results I have in cols BJ:BO. I have resulted the first 5 x names by filter.
To explain I will go to the name Billy Bunter as he has all expected results for all 6 x cols BJ:BO.
Each name is in ascending order by date, the oldest date to the newest date.
Col AP is counting how many starts each name has had.
Col BG is the number of days since each names last run, so this is looking back at the date of their last run and resulting how many days.
Each name is individual, and the results only refer to that one name.
There are names in here that have had only one start, so col BG will be 0, zero. There will not be any results in cols BJ:BO as there is nothing to refer back to.
The results in col BJ:BO are always referencing back over each name past history only.
Exact finish and exact margin.
Here we see Billy has in cell BG25 the number 11, this is an exact match in cell BG20, so results come from cols AL and AO. 4 and 3.3
Lower finish and lower margin.
This is looking back for the next smallest number in col BG.
Cell BG19 says 45, so looking back there is only one run, cell BG18 says 0, zero. So, results come from cols AL and AO being 15 and 8.6
Higher finish and Higher margin.
This is doing the same thing, except its looking for the closest number that is higher than the number in col BG.
Cell BG24 is 49, so the closest highest number to 49 is 109 in cell BG22. result in cols BN and BO are 5 and 3.5
I think I have explained everything, but I have a record of leaving things out, happy to take all questions and I hope I can get a result.
I have 580,000 rows to process, and I know some macros need time to work, I'm happy to let computer do its thing.
Thanks.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Trying to upload a sheet. please hold.
Appears sheet is too large, will have to come back when I have fixed problem. trying hard.
 
Upvote 0
Sorry for delay, using excel 2016 version. was unable to post whole sheet, this is short version.
BLANK WORKSHEET v3.xlsm
ABAHALAOAPBGBJBKBLBMBNBO
1IDDateNameFinishMarginStsDlstExact FinishExact MarginLower FinishLower MarginHigher FinishHigher Margin
21711/03/2023Billy Bunter158.610
31825/04/2023Billy Bunter20.4245158.6
4196/05/2023Billy Bunter43.3311158.620.4
52031/05/2023Billy Bunter812.842543.320.4
62117/09/2023Billy Bunter53.5510920.4
72211/10/2023Billy Bunter84.462443.3812.8
82329/11/2023Billy Bunter73.174920.453.5
92410/12/2023Billy Bunter10.481143.3158.684.4
Sheet1
 
Upvote 0
Code 1
Sub ProcessData()

Dim ws As Worksheet

Dim lastRow As Long

Dim i As Long



' Set the worksheet where your data is located

Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your actual sheet name



' Find the last row with data in column BJ (assuming BJ has continuous data)

lastRow = ws.Cells(ws.Rows.Count, "BJ").End(xlUp).Row



' Loop through each row

For i = 2 To lastRow ' Assuming your data starts from row 2 (header in row 1)

' Check if there is data in column BG (number of days since last run)

If ws.Cells(i, "BG").Value > 0 Then

' Call a function to process the data based on your conditions

ProcessRow ws, i

End If

Next i

End Sub



Sub ProcessRow(ws As Worksheet, rowNum As Long)

' Add your logic here to process each row based on the conditions you specified

' You can use ws.Cells(rowNum, "BJ") to ws.Cells(rowNum, "BO") to access the data in columns BJ to BO for the current row

' You can use ws.Cells(rowNum, "BG") to get the number of days since the last run



' Example:

Dim exactFinish As Double

Dim exactMargin As Double

Dim lowerFinish As Double

Dim lowerMargin As Double

Dim higherFinish As Double

Dim higherMargin As Double



' Add your logic to calculate the results based on your conditions

' ...



' Output the results to the desired columns (assuming columns BN and BO for this example)

ws.Cells(rowNum, "BN").Value = exactFinish

ws.Cells(rowNum, "BO").Value = exactMargin

End Sub

Code 2
Sub ProcessRow(ws As Worksheet, rowNum As Long)

Dim exactFinish As Double

Dim exactMargin As Double

Dim lowerFinish As Double

Dim lowerMargin As Double

Dim higherFinish As Double

Dim higherMargin As Double



' Get values from columns BJ to BO and BG for the current row

Dim BGValue As Double

Dim finishValue As Double

Dim marginValue As Double



BGValue = ws.Cells(rowNum, "BG").Value

finishValue = ws.Cells(rowNum, "BO").Value ' Assuming finish values are in column BO

marginValue = ws.Cells(rowNum, "BP").Value ' Assuming margin values are in column BP



' Calculate exact finish and margin based on BGValue

exactFinish = CalculateExactFinish(BGValue, finishValue)

exactMargin = CalculateExactMargin(BGValue, marginValue)



' Calculate lower finish and margin based on closest lower BGValue

Dim closestLowerBG As Double

closestLowerBG = FindClosestLowerBG(ws, rowNum, BGValue)

lowerFinish = CalculateLowerFinish(closestLowerBG, ws.Cells(rowNum, "BO").Value) ' Assuming finish values are in column BO

lowerMargin = CalculateLowerMargin(closestLowerBG, ws.Cells(rowNum, "BP").Value) ' Assuming margin values are in column BP



' Calculate higher finish and margin based on closest higher BGValue

Dim closestHigherBG As Double

closestHigherBG = FindClosestHigherBG(ws, rowNum, BGValue)

higherFinish = CalculateHigherFinish(closestHigherBG, ws.Cells(rowNum, "BO").Value) ' Assuming finish values are in column BO

higherMargin = CalculateHigherMargin(closestHigherBG, ws.Cells(rowNum, "BP").Value) ' Assuming margin values are in column BP



' Output the results to the desired columns (assuming columns BN and BO for this example)

ws.Cells(rowNum, "BN").Value = exactFinish

ws.Cells(rowNum, "BO").Value = exactMargin



' Optionally, you can output the results for lower and higher as well

' ws.Cells(rowNum, "BP").Value = lowerFinish

' ws.Cells(rowNum, "BQ").Value = lowerMargin

' ws.Cells(rowNum, "BR").Value = higherFinish

' ws.Cells(rowNum, "BS").Value = higherMargin

End Sub



Function CalculateExactFinish(BGValue As Double, finishValue As Double) As Double

' Replace this with your logic to calculate exact finish

CalculateExactFinish = finishValue ' Placeholder, replace with your actual logic

End Function



Function CalculateExactMargin(BGValue As Double, marginValue As Double) As Double

' Replace this with your logic to calculate exact margin

CalculateExactMargin = marginValue ' Placeholder, replace with your actual logic

End Function



Function FindClosestLowerBG(ws As Worksheet, rowNum As Long, BGValue As Double) As Double

' Replace this with your logic to find the closest lower value in column BG

' This is just a placeholder, replace it with your actual logic

FindClosestLowerBG = BGValue - 1

End Function



Function CalculateLowerFinish(closestLowerBG As Double, finishValue As Double) As Double

' Replace this with your logic to calculate lower finish

CalculateLowerFinish = finishValue ' Placeholder, replace with your actual logic

End Function



Function CalculateLowerMargin(closestLowerBG As Double, marginValue As Double) As Double

' Replace this with your logic to calculate lower margin

CalculateLowerMargin = marginValue ' Placeholder, replace with your actual logic

End Function



Function FindClosestHigherBG(ws As Worksheet, rowNum As Long, BGValue As Double) As Double

' Replace this with your logic to find the closest higher value in column BG

' This is just a placeholder, replace it with your actual logic

FindClosestHigherBG = BGValue + 1

End Function



Function CalculateHigherFinish(closestHigherBG As Double, finishValue As Double) As Double

' Replace this with your logic to calculate higher finish

CalculateHigherFinish = finishValue ' Placeholder, replace with your actual logic

End Function



Function CalculateHigherMargin(closestHigherBG As Double, marginValue As Double) As Double

' Replace this with your logic to calculate higher margin

CalculateHigherMargin = marginValue ' Placeholder, replace with your actual logic

End Function
 
Upvote 0

Forum statistics

Threads
1,223,871
Messages
6,175,092
Members
452,612
Latest member
MESTeacher

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top