Results 1 to 4 of 4

Thread: Changing Excel Files - Easy?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    148

    Exclamation

    Howdy!

    I want to update a test.xls file. I can open Excel97 and open the file I want, but how do you set the Sheet and add values per cell using vb6.

    here's what I have so far

    Dim MyXL As Object
    Dim ExcelWasNotRunning As Boolean

    Set MyXL = GetObject(, "Excel.Application")
    if Err.Number <> 0 Then ExcelWasNotRunning = True
    Err.Clear

    Set MyXL = GetObject("C:\test.xls")
    MyXL.Application.Visible = True
    MyXL.Parent.Windows(1).Visible = True

    For example I want to add "hello" to cell B13

    If you can help, Thanx


  2. #2
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Right, first off, have you set a reference to Excel? If not:

    Project/References, scroll down and check the Microsoft Excel 8.0 (or whatever) object library. Ok.

    Now for code:
    Code:
    Sub Main()
     Dim myXL As Excel.Application
    '*****************************
     Set myXL = CreateObject("Excel.Application")
     With myXL
      .Workbooks.Open "C:\Windows\Desktop\Fred.xls" 'Pick one of these
      .Workbooks.Add '                               Pick one of these
       .Range("B13") = "Hello"
      .ActiveWorkbook.SaveAs ("C:\Windows\Desktop\Fred.xls") 'Pick one of these
      .ActiveWorkbook.Save '                                  Pick one of these
     End With
     myXL.Quit
     Set myXL = Nothing
    End Sub
    Email me if you want any more help. But once you have this bit working, you could just go into excel and record anything you want to do, then copy and paste the code across.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    148

    Great

    That's great, but how do you determine which Sheet it is, that you are making the changes on. Example (Sheet 2)

  4. #4
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Put this in there somewhere:
    Code:
    .Sheets("Sheet2").Select
    Like I said, try opening Excel and recording stuff, like you could have recorded selecting sheet2 and you'd have got the code above...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width