I have a file, there're some numbers separated with "," example: 1, 5, 6, 8, 10. I have an integer it's equals 8 (int = 8). How to check if the int equals at least any number in that file?
Dim Mystring As String
Dim Stringlength As Integer
Dim Mynumbers() As Integer
j = 0
'load the file
Open App.Path & "\temp.txt" For Input As #1
'assuming your only using 1 line
Line Input #1, Mystring
Close #1
Stringlength = Len(Mystring)
'find your punctuation
For i = 1 To Stringlength
FoundValue = InStr(i, Mystring, ",", vbTextCompare)
If FoundValue = 0 Then
'not found
Exit For
Else
ReDim Preserve Mynumbers(j)
Mynumbers(j) = Mid(Mystring, i, FoundValue - i)
j = j + 1
i = FoundValue
End If
Next i
MyInt = 8
'validate what youve found
For i = 0 To j - 1
If Val(Mynumbers(i)) = MyInt Then
MsgBox "Found Value.", vbOKOnly, "Message"
End If
Next i
well...
compiller says Expected Array in this part:
"ReDim Preserve Mynumbers(j)"
here's a part of code, so you navigate better:
Else
ReDim Preserve Mynumbers(j)
Mynumbers(j) = Mid(Mystring, i, FoundValue - i)
j = j + 1
i = FoundValue
End If
hmmm... the code there was straight from a lil program i wrote (slightly modified) but i tested it and it worked... i pressume there may be something in your code that bounces of this code if it doesnt work. (to test you could start a new project and dump a command button on the form and see if it works.)
in the meantime ill take another look.
when you single step the code does the MyString value contain the numbers you want to search.
i honestly cant think why its not working (hopefully a tech will see this and give me a hand) I like doing it the hard way! OK!
this is straight from msdn (apologise if youve already read it)
Expected array
A variable name with a subscript indicates the variable is an array. This error has the following cause and solution:
The syntax you specified is appropriate for an array, but no array with this name is in scope.
Check to make sure the name of the variable is spelled correctly. Unless the module contains Option Explicit, a variable is created on first use. If you misspell the name of an array variable, the variable may be created, but not as an array.
For additional information, select the item in question and press F1.
I like doing it the hard way! OK!
pressed the button to early.... what i was going to add was that maybe you need to put the
Dim Mynumbers() As Integer
at the top of your code?!?!? I like doing it the hard way! OK!
hmmm...little problem actually I'm doing it not in VB, i'm using VBA (Visual BASIC for Applications) built-in MS Exel, could that be a reason i have errors?
This one builds a string giving the numbers element position in the string.
Option Explicit
Public CapString As String
Public Sub FindNumber()
Dim hfile As Long
Dim buff As String
Dim buffarray() As String
Dim sPathname As String
Dim intMyNbr As Integer: intMyNbr = 8
Dim intLo As Integer
Dim intHi As Integer
Dim intKnt As Integer
sPathname = App.Path + "\filein.txt"
hfile = FreeFile
Open sPathname For Input As #hfile
buff = Input$(LOF(hfile), hfile)
Close
buffarray = Split(buff, ","
intLo = LBound(buffarray)
intHi = UBound(buffarray)
For intKnt = intLo To intHi
If intMyNbr = CInt(buffarray(intKnt)) Then
CapString = "The number is in the " _
& Str(intKnt + 1) _
& " Element of the String"
Exit For
End If
Next intKnt
End Sub
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
hmm...I have one more question...
and if i want to make action when there's no 8 in that file, for example if there's no 8 in the file app shows msgbox ("There's no 8 in the file"
For i = 0 To j - 1
If Val(Mynumbers(i)) = MyInt Then
MsgBox "Found Value.", vbOKOnly, "Message"
End If
Next i
change to
dim MyFlag as boolean
MyFlag=false
For i = 0 To j - 1
If Val(Mynumbers(i)) = MyInt Then
MsgBox "Found Value.", vbOKOnly, "Message"
Myflag=true
End If
Next i
if MyFlag= false then
msgbox "Value not found.",vbokonly,"message"
end if
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.