Reading/Writing to excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hermitking
    New Member
    • Nov 2007
    • 30

    Reading/Writing to excel

    How do I make vb 2005 read and write to a specified cell in excell 2003 & 2007.
    Last edited by hermitking; Jan 9 '08, 04:30 PM. Reason: Grammer
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by hermitking
    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:
    [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
    Last edited by kadghar; Jan 9 '08, 11:13 PM. Reason: adding a note

    Comment

    • hermitking
      New Member
      • Nov 2007
      • 30

      #3
      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

      • hermitking
        New Member
        • Nov 2007
        • 30

        #4
        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

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by hermitking
          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?
          the second example reads only a cell

          [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

          HTH

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by hermitking
            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(.cel ls(1,1) ...

            Comment

            Working...