Input # Statement
Reads data from an open sequential file and assigns the data to variables.
Syntax :
Input #filenumber,
varlist
The
Input # statement syntax has these parts:
Part
|
Description
|
filenumber
|
Required. Any valid file number.
|
varlist
|
Required. Comma-delimited list
of variables that are assigned values read from the file — can't be an array or object variable.
However, variables that describe an element of an array or user-defined type
may be used.
|
Remarks:
Data
read with Input # is usually written to a file with Write #. Use
this statement
only with files opened in Input or Binary mode.When read, standard string or numeric data is assigned to variables without modification. The following table illustrates how other input data is treated:
Data
|
Value assigned to
variable
|
Delimiting comma or blank line
|
Empty
|
#NULL#
|
Null
|
#TRUE# or #FALSE#
|
True or False
|
#yyyy-mm-dd hh:mm:ss#
|
The date and/or time represented
by the expression
|
#ERROR errornumber#
|
errornumber (variable is a
Variant tagged as an error)
|
Note: Do not use embedded quotation
marks in file As Input # parses this string as two complete and separate
strings.
Data items in a file must appear in the same order as the variables in varlist
and match variables of the same data type.
If a variable is numeric and the data is not numeric, a value of zero is
assigned to the variable.
If you reach the end of the file while you are inputting a data item,
the input is terminated and an error occurs.
Note:To be able to correctly read data from a file into
variables using Input #, use the Write # statement instead of the Print
# statement to write the data to the files. Using Write # ensures each
separate data field is properly delimited.
Example:
This example uses the Input # statement to read data from a
file into two variables. This example assumes that
Sub inputstate()
Dim MyString, MyNumber
TxtFile = ActiveWorkbook.Path & "\Testfile.txt" ‘ the file which is being use to read data
Open TxtFile For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyString, MyNumber ' Read data into two variables.
MsgBox (MyString & "," & MyNumber)
Loop
Close #1 ' Close file.
End Sub
Dim MyString, MyNumber
TxtFile = ActiveWorkbook.Path & "\Testfile.txt" ‘ the file which is being use to read data
Open TxtFile For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyString, MyNumber ' Read data into two variables.
MsgBox (MyString & "," & MyNumber)
Loop
Close #1 ' Close file.
End Sub
Output 2:
Output 3:
How to write data into Notepad from Excel using VBA
No comments:
Post a Comment