TotallyConfused
Board Regular
- Joined
- May 4, 2017
- Messages
- 247
- Office Version
- 365
- Platform
- Windows
VBA Code:
Sub LoadColsIntoArray()
' This code worked perfect with no problem EXCEPT as noted in
' the TEST SECTION at the end.
Dim ws2 As Worksheet: Set ws2 = Sheets("Sheet2")
Dim Ary2 As Variant
Dim Ary2rows As Long
Dim Cols As String
Dim LastRow As Long
Application.ScreenUpdating = False
With ws2
Cols = "6,20" ' Only load columns 'F' and 'T' into Ary2 array
LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious, , , False).Row
Ary2 = Application.Index(Cells, Evaluate("ROW(5:" & LastRow & ")"), Split(Cols, ",")) ' Data starts in row #5
End With
' TEST SECTION:
' Okay, now to test whether everything was actually loaded into Ary2
Ary2rows = 1
' To confirm column 'F' was loaded into array, I added the following three lines. Everything worked perfect.
For Ary2rows = 1 To UBound(Ary2, 1)
MsgBox "Ary2(Ary2rows, 1) = " & Ary2(Ary2rows, 1)
Next
' To confirm column 'T' was loaded into array, I added the following three lines. Encountered a problem.
' The MsgBox displays ONLY 'John10' & 'John9' and nothing else. There were NO error messages.
' It's as if there were ONLY those two names in the column when loaded into Ary2.
'--- What is wrong that is causing this problem? ---
For Ary2rows = 1 To UBound(Ary2, 2)
MsgBox "Ary2(Ary2rows, 2) = " & Ary2(Ary2rows, 2)
Next
' Out of curiosity to see what would happen, I added the following three lines. This time, I used the value of
' UBound forAry2 column 1 as the loop counter, but then display values in column #2.
' In this case everything worked perfect with all 6 John's being displayed. This tells me that Column 'T' was
' completely loaded
For Ary2rows = 1 To UBound(Ary2, 1) ' NOTE: I'm accessing element #1 of the array, but
MsgBox "Ary2(Ary2rows, 2) = " & Ary2(Ary2rows, 2) ' displaying values from element #2
Next
' The following code displays the number 6 as it should
MsgBox "Ubound(Ary2,1) = " & UBound(Ary2, 1)
' The following code displays the number '2' for column 'T', but it should be the number '6'
MsgBox "Ubound(Ary2,2) = " & UBound(Ary2, 2)
Application.ScreenUpdating = True
End Sub
Row 1 | |||
Row 2 | |||
Row 3 | |||
Row 4 | |||
Row 5 | 0136 | John10 | |
Row 6 | 0145 | John9 | |
Row7 | 0134 | John8 | |
Row 8 | 015 | John7 | |
Row 9 | 0132 | John6 | |
Row 10 | 0131 | John5 |
THANK YOU in advance for any help you may be able to offer.
TotallyConfused
Last edited: