PDA

View Full Version : VBA - the lowest number not working



Eslay
07-07-2014, 11:29 PM
Hello i have a little problem with VBA, i make some mistake but i dont know where im beginner in VBA. I try to write program which ask me long shoul be string of numer and the i write every one numer. After this operation program should show numbers which i wrote and the lowest numer from this tab/string. Could you help me and tell me where i need to correct my code or correct this program by your owne ?



Function MinTablicy(TabCiag As Variant) As Integer
Dim WartMin As Integer
Dim IndeksDolny As Long, IndeksGorny As Long, i As Long
IndeksDolny = LBound(TabCiag)
IndeksGorny = UBound(TabCiag)
WartMin = TabCiag(IndeksDolny)
For i = IndeksDolny To IndeksGorny
If TabCiag(i) < WartMin Then WartMin = TabCiag(i)
Next
MinTablicy = WartMin
End Function


Sub CommandButton3_Click()
Dim ss As Long
Dim TabCiag() As Integer
Dim numerki As String ' wpisane numery
Dim i As Long

numerki = " "

ss = TextBox4.Text
If ss = 0 Then
MsgBox ("Ciag ma zerowa dlugosc ")
ElseIf ss < 0 Then
MsgBox ("Ciag nie moze byc ujemny")
Else
For i = 1 To ss
ReDim TabCiag(i)
TabCiag(i) = InputBox("Podaj liczbe " & i, "Liczba")
numerki = numerki + Str(TabCiag(i))

Next i

MsgBox "Wpisanow numery : " & numerki, , "LICZBY"
MsgBox "Minimalana wartość z tablicy to: " & MinTablicy(TabCiag)
End If

End Sub


Thx for your help

werafa
07-08-2014, 12:59 AM
Eslay,
my apologies if I have not understood your question correctly,

first, you can use ordinary excel functions in vba: e.g.

result = WorksheetFunction.Min(var1, var2)
will give you the minimum value in a set of numbers/array.

to write an array to a range (not sure if this is what you are trying to do, you could use a for/next loop.


Hope this helps you

Eslay
07-08-2014, 01:23 AM
thank you for your advice, it was very helpfull and i checked it. After I public this post on the other forum i get information from the other user that i should chceck my code with array range (if it correnct write in code or not) I make some changes an now it works as i want, below you can see the correct good working code. Thank one more time for help.




Function MinTablicy(TabCiag As Variant) As Integer
Dim WartMin As Integer
Dim IndeksDolny As Long, IndeksGorny As Long, i As Long
IndeksDolny = 1 'LBound(TabCiag)
IndeksGorny = UBound(TabCiag)
WartMin = TabCiag(IndeksDolny)
For i = IndeksDolny To IndeksGorny
If TabCiag(i) < WartMin Then WartMin = TabCiag(i)
Next
MinTablicy = WartMin
End Function

Sub CommandButton3_Click()
Dim ss As Long
Dim TabCiag() As Integer
Dim numerki As String ' wpisane numery
Dim i As Long

numerki = " "
ss = TextBox4.Text
If ss = 0 Then
MsgBox ("Ciag ma zerowa dlugosc ")
ElseIf ss < 0 Then
MsgBox ("Ciag nie moze byc ujemny")
Else
ReDim TabCiag(ss)
For i = 1 To ss

TabCiag(i) = InputBox("Podaj liczbe " & i, "Liczba")
numerki = numerki + Str(TabCiag(i))

Next i

MsgBox "Wpisanow numery : " & numerki, , "LICZBY"
MsgBox "Minimalana wartość z tablicy to: " & MinTablicy(TabCiag)
End If