473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Input # statement Visual Basic Editor (Excel)

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>
Nov 13 '06 #1
3 6665
willakawill
1,646 1GB
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))
Nov 13 '06 #2
I am also struggeling with dim coords. Has there been any major changes to the Excel 2010 structure?

-C.W.
Excel Statement
Apr 22 '11 #3
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.

Expand|Select|Wrap|Line Numbers
  1. Sub OpenTextFileTest()
  2. Dim myAtoms As String
  3. Dim myAtomArray() As String
  4. Dim i As Integer
  5.  
  6.     Const ForReading = 1, ForWriting = 2, ForAppending = 8
  7.     Dim fs, f
  8.     Set fs = CreateObject("Scripting.FileSystemObject")
  9.     Set f = fs.OpenTextFile("C:\Rodney\Atoms.txt", ForReading) 
  10.  
  11.     [A1].Select
  12.     Do While f.atendofstream <> True
  13.     myAtoms = f.readline
  14.  
  15.     myAtomArray = Split(myAtoms, vbTab) 'Make sure that you are using the correct _
  16.     delimiter value if it's a space use " " if it's a tab us vbTab or _
  17.     if it's somenthing else use it instead.
  18.  
  19.     For i = 0 To UBound(myAtomArray)
  20.         ActiveCell.Offset(0, i) = myAtomArray(i)
  21.     Next
  22.     ActiveCell.Offset(1, 0).Select
  23.     Loop
  24.     f.Close
  25.     set fs = nothing
  26. End Sub
  27.  
hope this helps.

RR
Apr 22 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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
2
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...
10
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...
27
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...
10
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...
1
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...
2
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...
1
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
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...
0
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...
0
Oralloy
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,...
0
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.