Hi everyone i am a total newbee in VBA and i am trying to lean about arrays. I have written the following code and get stuck in an infinite messagebox loop. Is there anyone that knows how to write the code and not get stuck in the loop?
Sub FindProduct()
Dim code() As String
Dim price() As Double
Dim NumProducts As Integer
Dim i As Integer
Dim p_code As String
'Read number of products:
NumProducts = Range("D1").Value
'Resize the arrays:
ReDim code(NumProducts)
ReDim price(NumProducts)
'Store information in the arrays:
For i = 1 To NumProducts
code(i) = Cells(i + 3, 1)
price(i) = Cells(i + 3, 2)
Next
p_code = InputBox("Please enter a product code:")
'Search in the code array:
For i = 1 To NumProducts
If code(i) = p_code Then
MsgBox "The price of product " & p_code & " is " & price(i)
Else
MsgBox "The product name is in the list "
End If
Next
End Sub
Sub FindProduct()
Dim code() As String
Dim price() As Double
Dim NumProducts As Integer
Dim i As Integer
Dim p_code As String
'Read number of products:
NumProducts = Range("D1").Value
'Resize the arrays:
ReDim code(NumProducts)
ReDim price(NumProducts)
'Store information in the arrays:
For i = 1 To NumProducts
code(i) = Cells(i + 3, 1)
price(i) = Cells(i + 3, 2)
Next
p_code = InputBox("Please enter a product code:")
'Search in the code array:
For i = 1 To NumProducts
If code(i) = p_code Then
MsgBox "The price of product " & p_code & " is " & price(i)
Else
MsgBox "The product name is in the list "
End If
Next
End Sub