VBA Print #1 Issues

SirCurious_VBAExcel

New Member
Joined
Sep 30, 2014
Messages
17
Hello, All,

I need help resolving a Print #1 issue. My code is written to export a text file based on a list box's results - it should only be printing 200 results. The issue is the exported text file contains only 199 (the 200th result appears as a blank line) - any suggestions?

My export subroutine:
(Public variable; "Dim ListArray(200) As Variant")
Code:
Private Sub Export_Click()
Dim OutCount As Integer
Dim OutFile As Variant


If ItemCount > 0 Then
    
    OutFile = Application.GetSaveAsFilename(fileFilter:="Text File(*.txt),*.txt")
        If OutFile <> False Then
        MsgBox "Save as" & OutFile
        End If
    
    Open OutFile For Output As #1
    For OutCount = 1 To ItemCount
        If OutCount < 201 Then
            Print #1, ListArray(OutCount)
        End If
    Next
    Close #1
     
    If ItemCount > 200 Then
        MsgBox ("Warning - list incomplete - only contains first 200 entries")
    End If
End If


End Sub
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,

Code:
Dim ListArray(200) As Variant
will give you an array which has elements numbered from 0 to 200, inclusive. That, is 201 elements in all.

Try:
Code:
Dim ListArray(1 To 200) As Variant
instead.
 
Upvote 0
Hi, RickXL,

Thanks for replying. Unfortunately this does not appear to be working. Any other ideas?

Is there something wrong with Print #1 - it's strange how only the last item in the array gets cut off.

-Alex
 
Upvote 0
Where does the value of ItemCount get set?

And what is it set to?

Also, what fills ListArray?

You could try using the "Locals Window" under the View menu to see how many elements of ListArray are being set and what to. I have to drill down on Module1 to find it.

If I put 200 values in I get 200 values out:
Code:
    Dim ListArray(1 To 200) As Variant
    For i = 1 To 200: ListArray(i) = i: Next
 
Last edited:
Upvote 0
Hi, RickXL,

Your question about what fills the listarray helped me find the solution! There was a condition in the sub that fills the listarray - "<200".

I switched it to "<201", and adjusted the dimensioned ListArray variable accordingly.

Thank you for your insight.

 
Upvote 0
No problem.

The number of times I have fixed a difficult computer problem just by describing it to someone else you would not believe!

Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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