Add sheet is workbook exists

richardcdahlgren

Board Regular
Joined
Oct 16, 2008
Messages
81
I have a code that creates a workbook and adds a sheet to it then saves. I need to adjust the code so it looks for the filename first and if it exists to just add the sheet to the existing file. Any and all help is greatly appreciated!! Code below:

Sub Button5_Click()

Dim filename As String
Dim path As String
Dim report As Workbook
Dim t As Range
Dim rs As Worksheet

Set report = ActiveWorkbook

filename = report.path & "\" & Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1)) & " " & "Ticket Review" & ".xlsx"

Application.ScreenUpdating = False

report.Sheets("Ticket Review").Range("A1:K125").Copy
Workbooks.Add
ActiveSheet.Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
ActiveSheet.Range("A1").PasteSpecial xlPasteFormats
For Each rs In Sheets
rs.Name = rs.Range("K3")
Next rs

ActiveSheet.SaveAs filename
Application.CutCopyMode = False
ActiveWorkbook.Close
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Something like this will work.

VBA Code:
Sub FileVoodoo()
Dim fs As Object, wb As Workbook

    Set fs = CreateObject("scripting.filesystemobject")
    
    strPath = "C:\TEST\Me.xlsm"
    
    If Not fs.FileExists(strPath) Then
    
        'do your new workbook stuff
        
    Else
    
        Set wb = Workbooks.Open(strPath)
        
        'do your existing workbook stuff
        
        wb.Close True, Path
    
    End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

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