473,757 Members | 10,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Input # statement Visual Basic Editor (Excel)

4 New Member
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.Get OpenFilename("A tom 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.Rang e("A" & lineNumber).Val ue = organicAtom
atomsSheet.Rang e("B" & lineNumber).Val ue = xCoord
atomsSheet.Rang e("C" & lineNumber).Val ue = yCoord
atomsSheet.Rang e("D" & lineNumber).Val ue = zCoord
lineNumber = lineNumber + 1
Loop
Close #1
End If
</code>
Nov 13 '06 #1
3 6682
willakawill
1,646 Top Contributor
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.Get OpenFilename("A tom 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.Rang e("A" & lineNumber).Val ue = organicAtom
atomsSheet.Rang e("B" & lineNumber).Val ue = xCoord
atomsSheet.Rang e("C" & lineNumber).Val ue = yCoord
atomsSheet.Rang e("D" & lineNumber).Val ue = 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(organicAt om, " ")
organicAtom = arData(0)
xCoord = CDbl(arData(1))
yCoord = CDbl(arData(2))
zCoord = CDbl(arData(3))
Nov 13 '06 #2
Grahm Schmidt
2 New Member
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
Rodney Roe
61 New Member
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
26242
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
2054
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 though I know I installed just about everything on the disk. So I went back to the setup on the disk and under custom there wasn't a visual basic option. Is it just that certain components are installed but not the VB editor? I'm kind of lost....
10
6756
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 opens up the code file, not the form designer). Also, it does not appear to recognize the code file....there are no outlining indicators, typing in the code file there is no Intellisense stuff, and the code does not compile saying that it doesnt...
27
35946
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 places inputBox returns a string so I could check for decimal point??? this seems like overkill The value returned can be asigned into an Integer type and then a Single
10
8204
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 everything I can find on COM Interop and .NET. I've also tried many of the 'Walkthroughs' on the MSDN site relating to COM add-ins, .NET and Office XP but am unable to get even these working in Excel 2002 or Word 2002. I've installed the Office XP...
1
3611
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 28,550 14,007 C 22,757 16,635 29,661 12,011 C 21,493 17,400 29,319 12,011 O 21,266 17,733 28,155 15,999 The columns B, C and D contain the coordinates of an organic atom with the name of the atom in column A. I would like to filter...
2
3931
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 "#Please do not delete this file". My guess is that would all be placed in the first variable, a$. The information held in your file is much more complex than can be handled by a single Input # statement. Probably your best bet is to keep looping and...
1
2909
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 button. Once pulling down the drive list and clicking to the specific drive, folders will be shown in the dir list. When clicking the folders in the dir list, files in that certain folder should apprear in the file list, but this program should limit...
7
2608
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 a table in Access. The affect of the multiple rows comes from a "CTRL return" I believe when creating the excel document. When I paste the excel data into a text document, the multiple rows in the excel spreadsheet appear as a single string with...
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.