Hello
I using the Rnd Command to prodruce 13 random numbers that are stored in a text file, I need help to produce a code that would look Through the text file for the largest number
Printable View
Hello
I using the Rnd Command to prodruce 13 random numbers that are stored in a text file, I need help to produce a code that would look Through the text file for the largest number
What is your text file looks like?
well it just a 13 lines with a random number on each line.
In that case you can do something like this but I'm sure there is a better way:
Code:Private Sub Command1_Click()
'============================
Dim sfile As String
Dim stext As String
Dim arLines() As String
Dim TempNumber As Single
Dim i As Integer
sfile = "c:\test.txt"
Open sfile For Input As #1
'read entire file into a string variable
'LOF is Length Of a File
stext = Input(LOF(1), #1)
Close 31
'populate array
arLines() = Split(stext, vbNewLine)
'loop through array
For i = 0 To UBound(arLines)
'check if entry is numeric
If IsNumeric(arLines(i)) Then
'check if new value is greater than previous
'CSng() is conversion function - ti converts string to single
If TempNumber < CSng(arLines(i)) Then
TempNumber = CSng(arLines(i))
End If
End If
Next i
Debug.Print TempNumber
End Sub
Thanks for the code can you explain a few things to me
I know EOF is but whats LOF
I dont know what Csng is?
I added comments to original post.
Here is another way of doing it where you read the file line by line and update the high value as needed.Code:Private Sub Command1_Click()
Dim strTextPath As String
Dim sngNum As Single 'holds the highest number
Dim strTemp As String 'holds the value of the current number in the file
'File with the numbers
strTextPath = "C:\numbers.txt"
'Open the file
Open strTextPath For Input As #1
'Read the lines one by one
Do While Not EOF(1)
Line Input #1, strTemp
'Check if the current number is larger than the previous high number
If CSng(strTemp) > sngNum Then
'Update the high number if the current number is larger
sngNum = CSng(strTemp)
End If
Loop
Close #1
MsgBox sngNum
End Sub
Thank you for your this code, can can anybody help me find the lowest number?
use the above function and just change the > to < like this...this should help you....vb Code:
If CSng(strTemp) < sngNum Then
Like ganeshmoorthy said use the code above but test for less than. You must also initialize a low number. In this example I'm taking the first number and using it to initialize sngNum.
Code:Private Sub Command1_Click()
Dim strTextPath As String
Dim sngNum As Single 'holds the highest number
Dim strTemp As String 'holds the value of the current number in the file
Dim blnRunning As Boolean
'File with the numbers
strTextPath = "C:\numbers.txt"
'Open the file
Open strTextPath For Input As #1
'Read the lines one by one
Do While Not EOF(1)
Line Input #1, strTemp
'Check if the current number is larger than the previous high number
'Initialize a starting low number
If Not blnRunning Then
sngNum = CSng(strTemp)
bnlrunning = True
End If
If CSng(strTemp) < sngNum Then
'Update the low number if the current number is lower
sngNum = CSng(strTemp)
End If
Loop
Close #1
MsgBox sngNum
End Sub