Finding min value of named range and then posting log entry causes 4 line to be written

gitmeto

Board Regular
Joined
Nov 24, 2021
Messages
60
I am attempting to a check for the min value of an array against and compare this to a value. It correctly determines the condition, posts the correct info on the last entry of the log but it proceeds to do this 4 times which is the same number of elements in the array.

The following table provides the array:
Stim1
17​
-1.07​
Stim2
22​
-1.07​
Stim3
27​
-0.48​
Control
70​
-0.06​

Here is the code that builds this array:

VBA Code:
Range("Data[Stim1 Targets]").End(xlDown).Offset(0, 1).Value = Range("Data[Stim1 Targets]").End(xlDown).Offset(0, 0).Value
Range("Data[Stim2 Targets]").End(xlDown).Offset(0, 1).Value = Range("Data[Stim2 Targets]").End(xlDown).Offset(0, 0).Value
Range("Data[Stim3 Targets]").End(xlDown).Offset(0, 1).Value = Range("Data[Stim3 Targets]").End(xlDown).Offset(0, 0).Value
Range("Data[Control Targets]").End(xlDown).Offset(0, 1).Value = Range("Data[Control Targets]").End(xlDown).Offset(0, 0).Value
Range("Data[Shams]").End(xlDown).Offset(0, 1).Value = Range("Data[Shams]").End(xlDown).Offset(0, 0).Value

Here is the code that checks the conditions:

VBA Code:
'Enter stimuli values configured in spreadsheet
Range("daily_stimuli[Intensity]").End(xlUp).Offset(1, 0).Value = exp_results.Range("G1").Value
Range("daily_stimuli[Intensity]").End(xlUp).Offset(2, 0).Value = exp_results.Range("G2").Value
Range("daily_stimuli[Intensity]").End(xlUp).Offset(3, 0).Value = exp_results.Range("I1").Value
Range("daily_stimuli[Intensity]").End(xlUp).Offset(4, 0).Value = exp_results.Range("I2").Value

'Enter Calculated dPrime values from spreadsheet
Range("daily_stimuli[dPrime]").End(xlUp).Offset(1, 0).Value = Range("Data[Stim1 dPrime]").End(xlDown).Offset(0, 0).Value
Range("daily_stimuli[dPrime]").End(xlUp).Offset(2, 0).Value = Range("Data[Stim2 dPrime]").End(xlDown).Offset(0, 0).Value
Range("daily_stimuli[dPrime]").End(xlUp).Offset(3, 0).Value = Range("Data[Stim3 dPrime]").End(xlDown).Offset(0, 0).Value
Range("daily_stimuli[dPrime]").End(xlUp).Offset(4, 0).Value = Range("Data[Control dPrime]").End(xlDown).Offset(0, 0).Value

Here is the conditions that it is properly matching:

VBA Code:
If Application.WorksheetFunction.Min(Range("daily_stimuli[dPrime]")) < Range("dPrime"). _
    Value And Application.WorksheetFunction.Max(Range("daily_stimuli[dPrime]")) < Range("dPrime").Value Then
        Range("Log[Date]").Offset(1, 0).Value = Date
        Range("Log[Time]").Offset(1, 0).Value = Format(Now(), "h:mm AM/PM")
        Range("Log[Description]").Offset(1, 0).Value = "d' Calculation"
        Range("Log[Notes]").Offset(1, 0).Value = "All stimulus d' values are below the d' threshold."
        'MsgBox "All stimulus d' values are below the d' threshold."
Exit Sub
End If

and finally here is the output that is written to the log after running the macro:

12/10/2021​
5:10 PM​
d' CalculationAll stimulus d' values are below the d' threshold.
1/0/1900​
d' CalculationAll stimulus d' values are below the d' threshold.
d' CalculationAll stimulus d' values are below the d' threshold.
All stimulus d' values are below the d' threshold.


Other than going back and cleaning up the log to delete that last 3 lines, is there something in the syntax of the code that could be written better to eliminate the need to do so?

Any assistance would contribute to the advancement of science.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Named ranges are useless when debugging something like this if you don't tell us the actual ranges that they refer to.

If you offset from a named range then the offset range will be the same size as the original range. If you want offset to a single cell from a larger range then you need to use the resize command with it as well.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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