mercredi 15 juillet 2020

VBA: My conditions ElseIf with <= operator are not returning true when they are expected to

I am using excel and vba to find the asymmetry of a gaussian curve. My data is stored in a column. In order to find the asymmetry I need to find the maximum (peak height) value and then find the values close to 1/10 of the max on either side of the curve. To do this I iterate once to find the max then iterate in two halves to find and set the values to a variable (closest101) corresponding to 1/10 the max. I am comparing the difference of the (1/10)max with the value in the cell and when a smaller difference is obtained, the value in the cell is the new closest match. I am not using Vlookup or Match function because my data is very noisy. The code for that is below:

'low is 1/10 max
For Each cell In halfRng
cell.Select
Debug.Print Abs(low - ActiveCell.Value)
    If Abs(fifty - ActiveCell.Value) <= difference50 Then
        difference50 = Abs(fifty - ActiveCell.Value)
        closest501 = ws.Range("C" & ActiveCell.Row).Value
    ElseIf Abs(low - ActiveCell.Value) <= difference10 Then
        difference10 = Abs(low - ActiveCell.Value)
        closest101 = ws.Range("C" & ActiveCell.Row).Value
        Debug.Print "new lowest difference  " & difference10
    End If
Next cell

I am also looking for the value at half the peak height, but that works fine with the same logic.

The problem that I am having is that the "logic" fails for values that should work. For example, a difference of 0.06 is less than the previous smallest difference which was 0.3. In this case, the ElseIf would be true and the new difference10 would be 0.06 but it behaves as if that condition is false. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire