Using Debug.Print to Print Array of Row Numbers

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Sorry if this posted already, I was having issues with it!


Hi There. I am having an issue with printing an Array to the immediate window using Debug.Print. I have a column heading containing HP1 and HP2 and another column heading containing HP3 and HP4. The ideal case is HP1=HP3 and HP2=HP4. I created a new column called HP5 and HP6 which should contain the correct value. This code checks to see if the ideal case is true and if it is, then enters those values in the new columns (and everything is okay). If it is not true, then the row number is added to the Array and at the end the Array should print out all the row values in the immediate window.

My problem is that it does not work as planned. All the cells are changed, but not all the row numbers from the array are printed out. I know that row 4 should be in the array but it does not show in the debug.print.

If I have a message box pop up and show each value in the array, the message box contains row 4 and has all the correct row values so I think I am doing something wrong with the debug.print. Thanks for any help!

Code:
<code class="bbcode_code">Sub CreateHPColumns()
    
    Dim RowNos()
    Dim i As Long, x As Long
    x = 0
        
        For i = 2 To 28415
            If Cells(i, 10) = Cells(i, 12) And Cells(i, 11) = Cells(i, 13) Then
                Cells(i, 14) = Cells(i, 12)
                Cells(i, 15) = Cells(i, 13)
            Else
                x = x + 1
                ReDim Preserve RowNos(x)
                RowNos(x) = i
            End If
        Next i
        
        For i = 1 To UBound(RowNos)
            Debug.Print (RowNos(i))
        Next i

End Sub</code>
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How big is the array?

The debug window can only show a certain amount of lines and since you are printing each value from the array on a new line the earlier items could be getting 'pushed' off.

If you want to view the entire contents of the array try this.
Code:
Debug.Print Join(RowNos, ", ")

That should print out the values in the array as a comma delimited list.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top