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

Click For Summary
SUMMARY

This discussion focuses on creating a VBA program to find the minimum value from a list of numbers stored in a text file. The user encountered errors in their initial code, specifically in the placement of the variable initialization and the condition for finding the lowest value. Key corrections included moving the initialization of the variable 'low' outside the nested loops and changing the condition to correctly reference the array indices. After implementing these changes, the user successfully resolved the issue.

PREREQUISITES
  • Understanding of VBA (Visual Basic for Applications) programming
  • Familiarity with array data structures in VBA
  • Basic file handling in VBA, including reading from and writing to text files
  • Knowledge of conditional statements and loops in programming
NEXT STEPS
  • Explore advanced array manipulation techniques in VBA
  • Learn about error handling in VBA to manage runtime errors effectively
  • Investigate file I/O operations in VBA for handling larger datasets
  • Study optimization techniques for improving the performance of VBA scripts
USEFUL FOR

This discussion is beneficial for beginner to intermediate VBA developers, data analysts working with text files, and anyone looking to enhance their skills in programming with arrays and file handling in VBA.

turkcyclone
Messages
9
Reaction score
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
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.
 
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!
 

Similar threads

Replies
1
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 10 ·
Replies
10
Views
26K