Microsoft KB Archive/119701

= Microsoft Knowledge Base =

XL: Visual Basic Input # Statement May Be Parsed Incorrectly
Last reviewed: May 30, 1997

Article ID: Q119701

5.00 5.00c 7.00 97 | 5.00 5.00a

WINDOWS           | MACINTOSH kbprg kbcode

The information in this article applies to:


 * Microsoft Excel for Windows, versions 5.0, 5.0c
 * Microsoft Excel for Windows 95, version 7.0
 * Microsoft Excel for the Macintosh, versions 5.0, 5.0a
 * Microsoft Excel 97 for Windows

SYMPTOMS
In Microsoft Excel, when you use the Input # statement, fields that have mixed number and text data values may be parsed incorrectly.

CAUSE
When Microsoft Excel reads the data using the Input # statement, it guesses the data type. So, when a field begins with numbers and also contains text, the field is parsed into two entries: one numeric and one text.

RESOLUTION
To correct this problem, do either of the following:

 Dimension variables as strings before you use them in the Input # statement -or- Use the Line Input # statement.

MORE INFORMATION
To keep Microsoft Excel from guessing the variable type, you need to declare the variable as string in the macro. This will force Microsoft Excel to look at each entry as a text string rather than predict the variable type.

Visual Basic Code Example
Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

The following example assumes that you have a text file called TEST.TXT, located in your current working directory. The contents of the file is as follows:

ABC Brand, 15" color television, $599 In a new module sheet enter in the following code:

Sub InputExample Dim a as String, b as String, c as String Open "test.txt" For Input Access Read Shared As #1 Input #1, a, b, c  MsgBox a   MsgBox b   MsgBox c   Close #1 End Sub To run the example, choose Start from the Run menu, or position the cursor in the line that reads "Sub InputExample" and press the F5 key.