Beginner VBA Help Using Arrays to find the lowest value from a list

In summary, the programmer is trying to find the Minimum value in a list of numbers, but keeps getting an error. They have changed some of the code so that it will only execute once, and it worked.
  • #1
turkcyclone
9
0
Hi, I have this text file.

"3 4
7.21 -5.7 0.01 9.0
5.6 0.11 -.123 14.
2.11 4.11 0.0001 -2. "

I am trying to create a VBA program to find the Minimum value, but I keep getting an error. Could someone help me? I am trying to get some practice for a test. Here is what I have so far...

Option Explicit
Sub test()

Dim A(1 To 3, 1 To 4) As Single, low, nrows, ncols As Single, i, j As Integer

Open ("C:\pathname\in_class_input_file.txt") For Input As #1
Open ("C:\pathname\in_class_output_file.txt") For Output As #2


Input #1, nrows, ncols
For i = 1 To nrows
For j = 1 To ncols
Input #1, A(i, j)

Next j
Next i

For i = 1 To nrows
For j = 1 To ncols

low = A(1, 1)

If A(i + 1, j) < low Then
low = A(i + 1, j)


Else

'Next j
'Next i


End If
Next j
Next i


Print #2, "The"; "lowest"; "Value Is"; low

Close #1
Close #2


End Sub
 
Technology news on Phys.org
  • #2
I put [ code] and [ /code] tags (without extra spaces) around your code, and adjusted your indentation.
turkcyclone said:
Hi, I have this text file.

"3 4
7.21 -5.7 0.01 9.0
5.6 0.11 -.123 14.
2.11 4.11 0.0001 -2. "

I am trying to create a VBA program to find the Minimum value, but I keep getting an error. Could someone help me? I am trying to get some practice for a test. Here is what I have so far...
Code:
Option Explicit
Sub test()

Dim A(1 To 3, 1 To 4) As Single, low, nrows, ncols As Single, i, j As Integer

Open ("C:\pathname\in_class_input_file.txt") For Input As #1
Open ("C:\pathname\in_class_output_file.txt") For Output As #2


Input #1, nrows, ncols
For i = 1 To nrows
    For j = 1 To ncols
    Input #1, A(i, j)
    
    Next j
Next i
    
For i = 1 To nrows
  For j = 1 To ncols

    low = A(1, 1)

    If A(i + 1, j) < low Then
       low = A(i + 1, j)
    
    Else

      'Next j
      'Next i
    End If

  Next j
Next i
    

Print #2, "The"; "lowest"; "Value Is"; low

Close #1
Close #2
    
End Sub

The statement low = A(1, 1) should be above the nested loops. As you have it, this statement executes 12 times. It should execute only once.
The statement If A(i + 1, j) < low Then should be changed to If A(i, j) < low Then. As you have it, when i is 3, you are evaluating A(4, j), which doesn't exist.
 
  • #3
Mark44 said:
I put [ code] and [ /code] tags (without extra spaces) around your code, and adjusted your indentation.

The statement low = A(1, 1) should be above the nested loops. As you have it, this statement executes 12 times. It should execute only once.
The statement If A(i + 1, j) < low Then should be changed to If A(i, j) < low Then. As you have it, when i is 3, you are evaluating A(4, j), which doesn't exist.


Thank you so much for your help and explanation. I did the changes and it worked like a charm!
 

Related to Beginner VBA Help Using Arrays to find the lowest value from a list

1. How do I create an array in VBA?

To create an array in VBA, use the Dim statement followed by the array name and the number of elements in parentheses. For example, "Dim myArray(10) As Integer" creates an array named myArray with 11 elements (0 to 10).

2. What is the purpose of using an array to find the lowest value?

Using an array allows you to store and access a list of values efficiently. In this case, you can loop through the elements of the array and compare them to find the lowest value without having to write separate code for each value.

3. How do I loop through an array in VBA?

You can use the For Each...Next statement to loop through an array in VBA. This statement will go through each element of the array and perform a specified action. For example, "For Each num in myArray" will loop through each element in the array named myArray.

4. Can I use an array to find the lowest value from a list stored in a worksheet?

Yes, you can use an array to find the lowest value from a list stored in a worksheet. You can first assign the range of cells containing the list to an array and then use that array to find the lowest value.

5. Is there a built-in function in VBA to find the lowest value from an array?

Yes, there is a built-in function called "Min" that can find the lowest value from an array in VBA. You can use this function by passing the array name as an argument, for example, "myMin = Min(myArray)". This will return the lowest value from the array myArray.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
11K
  • Programming and Computer Science
Replies
10
Views
25K
Back
Top