Closing & Opening Files

khwaja

New Member
Joined
Aug 19, 2005
Messages
14
I am using the following code to create a duplicate of an active file and then close the active file. But I am struggling to open the file I just created. Will appreciate some hand holding.

Sub Copy()
Dim strFilename As String
strFilename = ActiveWorkbook.Path & "" & "GJCT Roster Week " & Range("sWeekNo") + 1 & " WS " & Range("sWeekStart") + 7 & ".xlsm"
ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=52
ActiveWorkbook.Close
End Sub
 
think the reason for this. The macro is in the file I am closing, so it loses the ability to go further to open the file.

True. I missed the fact that the code was closing the active workbook before opening the new copy-workbook.

See if this works for you:

Code:
Sub Copy()
    Dim strFilename As String
    Dim strActiveWbName As Workbook
    
    strActiveWbName = ActiveWorkbook.Name
    strFilename = ActiveWorkbook.Path & "" & "GJCT Roster Week " & Range("sWeekNo") + 1 & " WS " & Range("sWeekStart") + 7 & ".xlsm"
    ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=52
    Application.OnTime Now + TimeSerial(0, 0, 2), "'OpenFile """ & strFilename & """,""" & strActiveWbName & "'"

End Sub

Sub OpenFile(ByVal FilePathName As String, ByVal ActiveWbName As String)
    Workbooks.Open (FilePathName)
    Workbooks(ActiveWbName).Close
End Sub
 
Last edited:
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Thanks. It has thrown up an runtime error 91: Object variable or With Block Variable not set and it stopping at:

strActiveWbName = ActiveWorkbook.Name
 
Upvote 0
Thanks. It has thrown up an runtime error 91: Object variable or With Block Variable not set and it stopping at:

strActiveWbName = ActiveWorkbook.Name

I figured it out. using the save as method overwrites the existing workbook so instead use save copy as. If the format of the copy doesn't change then just use save as on the newly opened workbook

Code:
Sub Copy()

Dim strFilename As String, D As Workbook ', K As Workbook

    Set D = ActiveWorkbook
    
    strFilename = D.Path & "\GJCT Roster Week " & Range("sWeekNo") + 1 & " WS " & Range("sWeekStart") + 7 & ".xlsm"    
    
   D.SaveCopyAs Filename:=strFilename 

   'Set K = Workbooks.Open (strFilename)
   
   Workbooks.Open (strFilename)
    
    D.Close

End Sub
 
Last edited:
Upvote 0
hey,
I am not sure if your problem is caused by the same issue i was having but maybe try this for closing the file down.

Code:
wb.Close SaveChanges:=True 
 Set wb = Nothing

'without this i find that sometimes the (i think) vba editor keeps files open that should be closed down.
 
Last edited:
Upvote 0
Thanks. It has thrown up an runtime error 91: Object variable or With Block Variable not set and it stopping at:

strActiveWbName = ActiveWorkbook.Name

Sorry - the variable should be declared as String :

Try this :
Code:
Sub Copy()
    Dim strFilename As String
    Dim strActiveWbName As String
    
    strActiveWbName = ActiveWorkbook.Name
    strFilename = ActiveWorkbook.Path & "" & "GJCT Roster Week " & Range("sWeekNo") + 1 & " WS " & Range("sWeekStart") + 7 & ".xlsm"
    ActiveWorkbook.SaveAs Filename:=strFilename, FileFormat:=52
    Application.OnTime Now + TimeSerial(0, 0, 2), "'OpenFile """ & strFilename & """,""" & strActiveWbName & "'"

End Sub

Sub OpenFile(ByVal FilePathName As String, ByVal ActiveWbName As String)
    Workbooks.Open (FilePathName)
    Workbooks(ActiveWbName).Close
End Sub
 
Upvote 0
I am again grateful for your help. I tried the following code and it worked.

Sub Copy()

Dim strFilename As String, D As Workbook ', K As Workbook

Set D = ActiveWorkbook

strFilename = D.Path & "\GJCT Roster Week " & Range("sWeekNo") + 1 & " WS " & Range("sWeekStart") + 7 & ".xlsm"

D.SaveCopyAs Filename:=strFilename

'Set K = Workbooks.Open (strFilename)

Workbooks.Open (strFilename)

D.Close

End Sub

The last one was producing an error.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top