473,320 Members | 2,048 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,320 software developers and data experts.

Reading/Writing to excel

How do I make vb 2005 read and write to a specified cell in excell 2003 & 2007.
Jan 9 '08 #1
5 2337
kadghar
1,295 Expert 1GB
How do I make vb 2005 read and write to a specified cell in excell 2003 & 2007.
have you searched around the forum? there are many examples of that. You can use an object with an excel application:
Expand|Select|Wrap|Line Numbers
  1. dim obj1 as object
  2. dim a as variant 'or object, i dont remember how it's defined in vb2005
  3. dim b as string
  4. set obj1 = createobject("excel.application") 'I think "set" is not used in vb 2005, so it'll remove it.
  5. obj1.workbooks.open "c:\mybook.xls"
  6. obj1.cells(1,1) = "hello":obj1.cells(1,2) = "goodbye"
  7. with obj1.worksheets(1)
  8.     a=range(.cells(1,1), .cells(1,1).end(-4121)) '-4121 is the number of the constant xldown, but it's not recognized by all the versions of vb, so -4121 or -4161 for xlleft will help.
  9.     b=.cells(1,1).value
  10. end with
you might have some troubles while closing the application, so what i'll recomend you to do, in order to avoid generating trash is to make it visible before you close it:

Expand|Select|Wrap|Line Numbers
  1. obj1.visible=true
  2. ob1.quit  'or was it close instead of quit?  i'll have to check that out.
HTH

Note: when you use obj1.cells(y,x) it'll asume you're using the "activebook" and "activesheet" the "right complete way" would be obj1.workbooks(1).worksheets(1).cells(1,1). Same for obj1.worksheets(1), it'll asume the workbook
Jan 9 '08 #2
Thanks for the help
With obj1.worksheets(1)
a = range(.cells(1, 1), .cells(1, 1).end(-4121)) '-4121 is the number of the constant xldown, but it's not recognized by all the versions of vb, so -4121 or -4161 for xlleft will help.
b = .cells(1, 1).value
End With

I get a message saying range is not declared.
Jan 9 '08 #3
I took range out and it works fine. Can you tell me how to read a cell now
this is how I read txt files
fileReader =
My.Computer.FileSystem.ReadAllText("C:\test.txt")
do i need to change the read all?
Jan 10 '08 #4
kadghar
1,295 Expert 1GB
I took range out and it works fine. Can you tell me how to read a cell now
this is how I read txt files
fileReader =
My.Computer.FileSystem.ReadAllText("C:\test.txt")
do i need to change the read all?
the second example reads only a cell

Expand|Select|Wrap|Line Numbers
  1. dim b as string
  2. b= obj1.cells(1,1)
while working with text files, i'd supose you want to read a tabs separated file. so the easiest way will be to save the file the way you're doing it, and then with the MID and INSTR functions take the "cell" you want.

Expand|Select|Wrap|Line Numbers
  1. fileReader=my.computer.filesystem.readalltext("c:\test.txt") 
  2. fileReader=mid(fileReader,1,instr(fileReader,chr(9))-1)
will give you the "first cell" of a tabs separated textfile

HTH
Jan 10 '08 #5
kadghar
1,295 Expert 1GB
Thanks for the help
With obj1.worksheets(1)
a = range(.cells(1, 1), .cells(1, 1).end(-4121)) '-4121 is the number of the constant xldown, but it's not recognized by all the versions of vb, so -4121 or -4161 for xlleft will help.
b = .cells(1, 1).value
End With

I get a message saying range is not declared.
I see, i think it's because range is a VBA function, try with

a= obj1.range(.cells(1,1) ...
Jan 10 '08 #6

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

Similar topics

6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
3
by: Jon Brookins | last post by:
I'm reposting this question as the other post seems to have been sucked into the net black hole. I'll keep this short. I am trying to read and edit an Excel workbook in its native BIFF format. ...
2
by: Rashrashetta | last post by:
Hai all, I am trying to read and write to an excel sheet using C# in a webform... I'v read alot of tutorials and used it...it works BUT ONLY ONCE...Especially when writing to the sheet... I...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
13
by: nuti | last post by:
Hi all, I am fairly new to VB.I am trying to figure out as how to write a script so that i can read the data from an excel sheet to Access. can u guys please help me out? cheers, nuti
3
by: RJN | last post by:
Hi I've a template excel file which has all the calculations defined. There are certain input values to be entered which gives a lot of output to the user. I don't want to expose the excel sheet...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
2
by: i.arda.tezcan | last post by:
Hello, I'm trying to make a web page that will read the xls file and make a table out of it on the web page. I want to change the excel file from time to time and I want the changes to be...
1
by: Tony Bansten | last post by:
As fas as I heard I can write to Excel cell e.g. E53 from outside with VBS. How could such a code look like? Assume I have an Excel file aaa.xls and a worksheet "ws777" inside: How can I wread...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.