I am trying to get a macro to perform an action which finds the maximum value in one cell that translates to a minimum value in another cell. The macro works for a single cell (see code below), however, when I added in a for loop, it sets the first value to zero and then stops running.
The part I seem to be stuck on is offsetting the ChangeVal and Answer ranges by one cell and starting the whole thing over. My two biggest concerns are that the for loop is in the wrong place, or that the way i am trying to offset the cells is incorrect. If anyone has any ideas, I would really appreciate it.
The part I seem to be stuck on is offsetting the ChangeVal and Answer ranges by one cell and starting the whole thing over. My two biggest concerns are that the for loop is in the wrong place, or that the way i am trying to offset the cells is incorrect. If anyone has any ideas, I would really appreciate it.
Code:
Sub macro()
Dim ChangeVal As Range, Answer As Range
Dim j As Long
Dim i As Double
Dim xmax As Double
'cell being modified
Set ChangeVal = ActiveWorkbook.Sheets("ITC").Range("C341")
Set Answer = ActiveWorkbook.Sheets("ITC").Range("C892")
For j = 1 to 3
ChangeVal.Value = 0
xmin = ChangeVal
'cell being evaluated
min_value = Answer
y = Answer
'increment
i = 0.1
'max x value
xmax = Range("max").Value
diff = (xmax - xmin) / 2
Do While i <= diff
xmin = xmin + diff
ChangeVal = xmin
Z = Answer
If Z > y Then
xmin = xmin - diff
xmax = xmin + diff
ChangeVal = xmin
y = Answer
Else
xmin = xmin - diff / 2
ChangeVal = xmin
y = Answer
End If
diff = (xmax - xmin) / 2
Loop
ChangeVal = xmin
Set ChangeVal = ChangeVal.Offset(0, 1)
Set Answer = Answer.Offset(0, 1)
Next j
End Sub