How do I make vb 2005 read and write to a specified cell in excell 2003 & 2007.
Reading/Writing to excel
Collapse
X
-
Tags: None
-
have you searched around the forum? there are many examples of that. You can use an object with an excel application:Originally posted by hermitkingHow do I make vb 2005 read and write to a specified cell in excell 2003 & 2007.
[CODE=vb]
dim obj1 as object
dim a as variant 'or object, i dont remember how it's defined in vb2005
dim b as string
set obj1 = createobject("e xcel.applicatio n") 'I think "set" is not used in vb 2005, so it'll remove it.
obj1.workbooks. open "c:\mybook. xls"
obj1.cells(1,1) = "hello":obj1.ce lls(1,2) = "goodbye"
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).v alue
end with[/CODE]
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:
[CODE=vb]obj1.visible=tr ue
ob1.quit 'or was it close instead of quit? i'll have to check that out.[/CODE]
HTH
Note: when you use obj1.cells(y,x) it'll asume you're using the "activebook " and "activeshee t" the "right complete way" would be obj1.workbooks( 1).worksheets(1 ).cells(1,1). Same for obj1.worksheets (1), it'll asume the workbook -
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.Comment
-
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.Fil eSystem.ReadAll Text("C:\test.t xt")
do i need to change the read all?Comment
-
the second example reads only a cellOriginally posted by hermitkingI 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.Fil eSystem.ReadAll Text("C:\test.t xt")
do i need to change the read all?
[CODE=vb]dim b as string
b= obj1.cells(1,1)[/CODE]
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.
[CODE=vb]
fileReader=my.c omputer.filesys tem.readalltext ("c:\test.tx t")
fileReader=mid( fileReader,1,in str(fileReader, chr(9))-1)[/CODE]
will give you the "first cell" of a tabs separated textfile
HTHComment
-
I see, i think it's because range is a VBA function, try withOriginally posted by hermitkingThanks 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.
a= obj1.range(.cel ls(1,1) ...Comment
Comment