Save and close a wb

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,392
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have some code that is meant to save and close the workbook identified in the select statement (Wes_reference or Riv_reference) but it is not closing it. What have I left out?



Code:
Sub AddReference()

Dim wb1 As Workbook, wb2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet, site As String
Dim f As Range, client As Variant, Ref As String, ref2 As String

    Set wb1 = ThisWorkbook
    Set sh1 = wb1.Worksheets("quote_sheet")
    'set site = site name selected on Start_here sheet
    site = ThisWorkbook.Worksheets("Start_here").Range("H9")
    'open the reference number sheet for the site that has been selected on the sheet Shart_here
    Select Case site
        Case "Wes"
                If Not isFileOpen("Wes_reference.xlsm") Then
                    Workbooks.Open ThisWorkbook.Path & "\" & "Wes_reference.xlsm"
                End If
            Set wb2 = Workbooks("Wes_reference")
            Set sh2 = wb2.Sheets("Wes_reference")
        Case "Riv"
                If Not isFileOpen("Riv_reference.xlsm") Then
                    Workbooks.Open ThisWorkbook.Path & "\" & "Riv_reference.xlsm"
                End If
            Set wb2 = Workbooks("Riv_reference")
            Set sh2 = wb2.Sheets("Riv_reference")
    End Select
    
    Ref = sh2.Cells(Rows.Count, 1).End(xlUp).Value
    ref2 = Ref + 1
    
    sh2.Cells(1, 1).End(xlDown).Offset(1, 0).Value = ref2
    sh1.Range("H5").Value = ref2
    With wb2
        .Save
        .Close
    End With
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try setting the workbook when you open it

Replace
Code:
Workbooks.Open ThisWorkbook.Path & "\" & "Wes_reference.xlsm"
with
Code:
Set wb2 = Workbooks.Open(ThisWorkbook.Path & "\" & "Wes_reference.xlsm")
and delete
Code:
Set wb2 = Workbooks("Wes_reference")

Same for the other workbook


Incidentally to set workbook after opening it, this may be the problem
Code:
Set wb2 = Workbooks("Wes_reference")
Should be
Code:
Set wb2 = Workbooks("Wes_reference.xlsm")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,665
Members
452,992
Latest member
TokugawaIesuma

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