B
B
bitkoljas2021-05-07 19:18:22
Visual Basic
bitkoljas, 2021-05-07 19:18:22

What is the correct way to find the smallest value passed to VBA?

For several hours I can’t figure out what the problem is
. If I insert this script into Excel and start passing values ​​​​of 15 values ​​\u200b\u200b(positive, negative and 0) into it, all the code works properly except for finding the smallest value from the transferred ones, for some reason the script works properly only with positive values, or only with negative ones, when passing to negative positive or 0 everything breaks down, how can I fix the problem and find the smallest value from the transferred ones?

The code:

Sub Methods()
Dim Number, Minimum, Room, Product, Data, X As Double
Dim Flag As Boolean

Flag = True

Minimum = 0
Room = 0
Product = 0

Number = InputBox("Введите количество значений в последовательности")
For X = 1 To Number Step 1
    Data = InputBox("Введите значение №" & X)
    If Data > Minimum Then
        Minimum = Data
        Room = X
    End If

    If X = 1 Then
        Minimum = Data
        Room = X
    End If
    
    If Data < 0 Then
        If Flag = True Then
            Product = Data
            Flag = False
        Else
            Product = Product * Data
        End If
    End If
    
Next X
MsgBox "Наименьшее число: " & Minimum & Chr(10) & "Его номер: " & Room & Chr(10) & "Произведение всех отрицательных значений: " & Product
End Sub


Tried to do it like this but it works intermittently
The code:

If Data < Minimum Then
        Minimum = Data
        Room = X
Else
        If Data = 0 Then
            If Not Minimum = 0 Then
                If Minimum > 0 Then
                    Minimum = Data
                    Room = X
                End If
            End If
        Else
            If Data < Minimum Then
                Minimum = Data
                Room = X
            End If
        End If
End If


Help me to understand

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question