Correct VB code to close a workbook

  • Firstly I hope I've placed this in the correct forum.


    Secondly, I apologise for the very messy code below, its a result of 'gathering' various other code and recording macros to get this to do what I require.


    I've got a workbook, with a couple of worksheets in, 1 of which is a dashboard, so the macro below grabs the staff listed in the other worksheet copies them to a new one then filters the list and returns them, once its done its sorts into alaphbetical order and then copies a formula down each adjacent cell.


    As I'm sure many will tell, I'm not great with VB scripting, but this does work, it worked much better before I remembered I had to share the workbook, hence the copy out to a new sheet. The bit I'm stuck on is

    Code
    Workbooks(Temp).Close SaveChanges:=False

    For some reason it just errors, I've tried various combinations but I just can't get it to work.


    Any help greatly appreciated, the whole script is below.


    Thanks


    Nick

  • Re: Correct VB code to close a workbook


    Code
    set Wbtemp=Workbooks.Open Filename:=Temp
    ......................
    .....................
        Wbtemp.Close SaveChanges:=False
  • Re: Correct VB code to close a workbook


    I'm not really sure why you bother with an additional workbook, but perhaps this will work (air code):

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: Correct VB code to close a workbook


    I managed to fix it, I realised where I was going wrong too.


    Code
    FileToOpen = ActiveCell


    That was bringing back the whole file name e.g. c:dir/dir/filename.xls


    Where as what I needed was just filename.xls


    so I just added this within the code when its in the workbook I need, I know its probably a bit sloppy still but it works for me;


    Code
    temp = ActiveWorkbook.Name
  • Re: Correct VB code to close a workbook


    Hi Rory, thanks for the reply. Is there an easier whay to do it then without opening a 'temp' workbook. I only did this as the once the workbook is shared the marco stoppped working.


    And thanks for the above code, it does indeed solve the problem :)


    Nick

  • Re: Correct VB code to close a workbook


    You should be able to simply filter straight from one sheet in the workbook to the other. Although now that you mention shared workbooks (shudder) all bets are off... ;)

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: Correct VB code to close a workbook


    Yeah I know, not ideal.


    I guess I could just create a new sheet then delte or not save the new sheet once the working out has finished?


    Nick

  • Re: Correct VB code to close a workbook


    So, just in case it helps anyone else out, I ended up with this.


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!