I'm quite a newbie to Visual Basic and have a problem with the Input # statement in the Visual Basic Editor from Excel. I would like to read in a tab delimeted text file selected by a user with records like this:
<snip>
O 21.266 17.733 28.155
N 20.649 17.703 30.320
C 20.883 17.336 31.723
</snip>
I'm using the code below to read each line from the file and store them in the variables organicAtom, xCoord, yCoord and zCoord. The problem is that by using a String as type for the variable organicAtom, it's value for the first line becomes "N 23.179 15.744 28.550". If I replace the type of organicAtom by a Long then each value will be 0 but my other variables xCoord, yCoord and zCoord are read in well.
I don't see any extra arguments in the Input # statement that can prevent this behavior.
<code>
Dim atomsTextFile As Variant
atomsTextFile = Application.GetOpenFilename("Atom text files (*.txt), *.txt", 1, "Select atoms with reduced coordinates input file")
Dim lineNumber As Integer Dim organicAtom As String
Dim xCoord, yCoord, zCoord As Double
If atomsTextFile <> False Then
Open atomsTextFile For Input Access Read As #1
lineNumber = 1
Do While Not EOF(1) Input #1, organicAtom, xCoord, yCoord, zCoord
atomsSheet.Range("A" & lineNumber).Value = organicAtom
atomsSheet.Range("B" & lineNumber).Value = xCoord
atomsSheet.Range("C" & lineNumber).Value = yCoord
atomsSheet.Range("D" & lineNumber).Value = zCoord
lineNumber = lineNumber + 1
Loop
Close #1
End If
</code>
3 6665
I'm quite a newbie to Visual Basic and have a problem with the Input # statement in the Visual Basic Editor from Excel. I would like to read in a tab delimeted text file selected by a user with records like this:
<snip>
O 21.266 17.733 28.155
N 20.649 17.703 30.320
C 20.883 17.336 31.723
</snip>
I'm using the code below to read each line from the file and store them in the variables organicAtom, xCoord, yCoord and zCoord. The problem is that by using a String as type for the variable organicAtom, it's value for the first line becomes "N 23.179 15.744 28.550". If I replace the type of organicAtom by a Long then each value will be 0 but my other variables xCoord, yCoord and zCoord are read in well.
I don't see any extra arguments in the Input # statement that can prevent this behavior.
<code>
Dim atomsTextFile As Variant
atomsTextFile = Application.GetOpenFilename("Atom text files (*.txt), *.txt", 1, "Select atoms with reduced coordinates input file")
Dim lineNumber As Integer Dim organicAtom As String
Dim xCoord, yCoord, zCoord As Double
If atomsTextFile <> False Then
Open atomsTextFile For Input Access Read As #1
lineNumber = 1
Do While Not EOF(1) Input #1, organicAtom, xCoord, yCoord, zCoord
atomsSheet.Range("A" & lineNumber).Value = organicAtom
atomsSheet.Range("B" & lineNumber).Value = xCoord
atomsSheet.Range("C" & lineNumber).Value = yCoord
atomsSheet.Range("D" & lineNumber).Value = zCoord
lineNumber = lineNumber + 1
Loop
Close #1
End If
</code>
Hi. first of all, when you are declaring variables as in;
Dim xCoord, yCoord, zCoord As Double
only zCoord will be a Double type. The rest will be variants.
Read the text into organicAtom
Input #1, organicAtom
Then use Split()
Dim arData As Variant
arData = Split(organicAtom, " ")
organicAtom = arData(0)
xCoord = CDbl(arData(1))
yCoord = CDbl(arData(2))
zCoord = CDbl(arData(3))
I am also struggeling with dim coords. Has there been any major changes to the Excel 2010 structure?
-C.W. Excel Statement
You shouldn't even need to dimension your coordinate if you are imputing them right into Excel. Here is an example code,
This code uses Textstream instead of input/output I think it's easier to read and use. -
Sub OpenTextFileTest()
-
Dim myAtoms As String
-
Dim myAtomArray() As String
-
Dim i As Integer
-
-
Const ForReading = 1, ForWriting = 2, ForAppending = 8
-
Dim fs, f
-
Set fs = CreateObject("Scripting.FileSystemObject")
-
Set f = fs.OpenTextFile("C:\Rodney\Atoms.txt", ForReading)
-
-
[A1].Select
-
Do While f.atendofstream <> True
-
myAtoms = f.readline
-
-
myAtomArray = Split(myAtoms, vbTab) 'Make sure that you are using the correct _
-
delimiter value if it's a space use " " if it's a tab us vbTab or _
-
if it's somenthing else use it instead.
-
-
For i = 0 To UBound(myAtomArray)
-
ActiveCell.Offset(0, i) = myAtomArray(i)
-
Next
-
ActiveCell.Offset(1, 0).Select
-
Loop
-
f.Close
-
set fs = nothing
-
End Sub
-
hope this helps.
RR
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JW |
last post by:
I am looking for sample code to learn me about using excel from a visual
basic program.
I want to open a file, store code in a certain cell and save this again.
Who can help me
Regards
|
by: PkingTom |
last post by:
I have read that Visual Basic 6.0 is included with Visual Studio 6.0
Professional Edition. Maybe this is a dumb question but how do I bring up the
VB Editor? I wasn't sure if I installed it even...
|
by: Dave Taylor |
last post by:
I have a VB.NET WinForms project that has about 15 forms in it. When I
opened the project yesterday, one of the form files does not open as a form
(i.e. double-clicking it in Solution Explorer...
|
by: code_wrong |
last post by:
Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?
isNumeric() checks for a numeric value .. but does not notify of numbers
with decimal...
|
by: Steve |
last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains
custom functions. I believe I need to use COM interop to allow VBA code in
Excel 2002 to access it. I've studied...
|
by: marknoten |
last post by:
I have an excel file with columns A, B, C, D and E that look like this:
A B C D E
-----------------------------------------------------
N 23,179 15,744...
|
by: Killer42 |
last post by:
The Input #1 statement simply reads in one line from a text file (in this case you INI file) and places the values from it into one or more variables. So what you are reading in this statement is...
|
by: chrspta |
last post by:
I am new to Visual basic. I need a program using VB6 that converts txt files to excel file.Description is in the below:
The form should have the Drive list, Dir list, file list and cmdConvert...
|
by: NJonge01 |
last post by:
I would like to take some data that I've cut and paste from an Excel document. the editor of the excel document has in single cells multiple "rows" of data that I would like to put on single rows of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |