Consulting

Results 1 to 3 of 3

Thread: sequential open and close workbooks

  1. #1

    sequential open and close workbooks

    Hi I have two workbooks wkb1 ,wkb2,

    I wanna open them at different scheduled time ,but application.ontime can only be written once in Excel.
    so I am trying to open wkb1 on schedule time, then before it closes itself, wkb1 open wkb2. wkb2 will first close wkb1 ,then run the rest of the code. The problem is when wkb2 close wkb1, nothing continues...looks like everything stops as wkb1 closes

    wkb1 code
    [vba]
    Private Sub Workbook_Open()
    Range("a1").Value = 1
    Workbooks.Open Filename:="C:\wkb2.xls"
    End Sub
    [/vba]

    wkb2 code

    [vba]
    Private Sub Workbook_Open()

    Dim wkb1 As Workbook

    set wkb1 = workbooks("wkb1.xls")
    wkb1.save
    wkb1.close ' Here is where everything stops...the rest of the code doesn't run

    Workbooks("wkb2.xls").Activate
    ActiveSheet.Range("a1").Value = 2
    ThisWorkbook.Close

    End Sub
    [/vba]
    please advise on this

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You can create as many Ontimes as you want, there is no issue with that.

    Or you could use application events to control it.

    But simplest of all, don't close wkb1 in wkb2, wait until it opens and closes itself, then close wkb1. There seems no reason in your code for doing it the way you are.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    The wkb1 code is still operating the Open command when wkb2 closes it. That's why everything stops at that point. You'll need to take a different approach.

Posting Permissions

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