If sheet exists then...

trevolly

Board Regular
Joined
Aug 22, 2021
Messages
120
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a workbook we use at work for a daily log and there are two versions - there is a Day and Night version. In this workbook is a worksheet called "Handover" that we copy from Day to Night work book and so on... I've put a button in to each workbook and attached a vba code to it that copies the "Handover" worksheet, opens the Night workbook, pastes the "Handover" sheet in to the Night workbook and then saves this workbook and close it.

The issue I have is that if a colleague presses the button multiple times then Excel copies the "Handover" sheet multiple times in to the Night workbook.... not knowing that the "Handover" worksheet has already been copied and pasted. This results in multiple "Handover" sheets (Handover2, Handover3) ect...

Would anyone be able to help as I'd like the vba code to be able to check if the "Handover" sheet already exists in the closed workbook and if so Excel doesn't copy the "Handover" sheet again and shows a message box saying it already exists.

Ive attached the vba code that was written for me - I'd really appreciate any help

VBA Code:
Sub CopySheetDaytoNight()
'Handover Copy Day to Night
        Dim Ws As Worksheet
        Set Ws = Worksheets("Handover")
        Application.ScreenUpdating = False
    With Workbooks.Open(Replace(ActiveWorkbook.FullName, " Day.", " Night."))
         Ws.Copy , .Sheets("Closures")
        .Close True
    End With
        Application.ScreenUpdating = True
        Set Ws = Nothing
        MsgBox "Handover Sheet Copied"
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi trevolly,

Not fully tested but this should do the trick:

VBA Code:
Option Explicit
Sub CopySheetDaytoNight()

    'Handover Copy Day to Night
    Dim Ws As Worksheet
    Dim wsTemp As Worksheet
    Dim wb As Workbook
    
    Application.ScreenUpdating = False
    
    Set Ws = Worksheets("Handover")
    Set wb = Workbooks.Open(Replace(ActiveWorkbook.FullName, " Day.", " Night."))
        
    On Error Resume Next
        Set wsTemp = wb.Sheets(CStr(Ws.Name))
        If Err.Number <> 0 Then
            Ws.Copy , wb.Sheets("Closures")
            wb.Close True
            MsgBox "Handover Sheet Copied"
        Else
            wb.Close False 'Close without saving any changes
            MsgBox "Tab """ & Ws.Name & """ already exists in workbook """ & wb.Name & """.", vbExclamation
        End If
    On Error GoTo 0
    
    Set wb = Nothing: Set Ws = Nothing: Set wsTemp = Nothing
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution
Hey @Trebor76, thank you so much for the reply - this works great. This'll stop the colleagues who mash the Handover copy button and then wonder why there's multiple copies and then blame the form!!!

Thank you again.

Trevor
 
Upvote 0
Hey @Trebor76, thank you so much for the reply - this works great. This'll stop the colleagues who mash the Handover copy button and then wonder why there's multiple copies and then blame the form!!!

Thank you again.

Trevor

Thanks for letting us know and you're welcome :)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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