+ Reply to Thread
Results 1 to 4 of 4

windows(myfile).close not working with 2 files

  1. #1
    Registered User
    Join Date
    07-12-2013
    Location
    Jersey
    MS-Off Ver
    Excel 2010
    Posts
    59

    windows(myfile).close not working with 2 files

    Hi
    I have written a vb macro to open files so that I can compare every file with every other. This leaves me with the files open. When trying to close a file I have successfully used
    windows(myfile1).close
    my problem is that when trying to close the other file that is open I get an error after using
    windows(myfile2).close

    Hope you can help

    kevin

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: windows(myfile).close not working with 2 files

    It is better to use:
    Please Login or Register  to view this content.
    and myfile2 must contain file name including extension.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Registered User
    Join Date
    07-12-2013
    Location
    Jersey
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: windows(myfile).close not working with 2 files

    Hi

    Still having problems with the line Workbooks(myfile2).Close below.

    It does close the file but then brings up an error.


    Sub CoV()

    mypath = Range("B3")
    rightdistance = 7
    rightcount = rightdistance
    verticaldistance = 2

    NxF1:
    Windows("CoVariance.xlsm").Activate
    Application.ScreenUpdating = False
    xaxisname = Cells(1, rightcount).Address
    yaxisname = Cells(verticaldistance, 6).Address
    myfile1 = Range(xaxisname)
    myfile2 = Range(yaxisname)
    Workbooks.Open Filename:=mypath & myfile1
    Workbooks.Open Filename:=mypath & myfile2
    Windows("CoVariance.xlsm").Activate
    Cells(verticaldistance, rightcount).Formula = "=PEARSON(" & myfile1 & "!$F$1:$F$1000," & myfile2 & "!$F$1:$F$1000)"

    Workbooks(myfile1).Close

    Workbooks(myfile2).Close

    rightcount = rightcount + 1


    If rightcount = 10 Then
    'Workbooks(myfile1).Close

    Application.ScreenUpdating = True
    verticaldistance = verticaldistance + 1
    rightdistance = rightdistance + 1
    rightcount = rightdistance


    If verticaldistance = 5 Then

    Exit Sub

    End If
    End If

    GoTo NxF1
    Application.ScreenUpdating = True
    'Windows(myfile1).Close
    End Sub

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Try creating references to the workbooks when you open them, you can then use those references when closing them.
    Please Login or Register  to view this content.
    PS Can you use code tags when posting code?
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Working with WorkBook Close event and Personal.xlsb files
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-09-2013, 12:40 AM
  2. Macros created in Windows XP not opening /working in Windows 7
    By Janane in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2012, 04:28 AM
  3. auto close windows
    By omgeokid in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-25-2011, 12:34 PM
  4. [SOLVED] Windows XP : Windows won't boot up, how to back up files?
    By jesika in forum Microsoft Windows Help
    Replies: 5
    Last Post: 11-30-2009, 06:03 AM
  5. Close all Windows?
    By nbaj2k in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2006, 09:59 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1