Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15064

copying single sheet to a new workbook vb.net

$
0
0
Dear All

I am working on a code to copy specific sheet to a new work book in excel but the code copies all the sheets although I define the sheet by name, My code is as below:

Code:

Private Sub ExportStockReport_T08()
        Dim xl As New Excel.Application
        Dim wb As Excel.Workbook
        Dim ws As Excel.Worksheet
        Dim SaveToDirectory As String
        SaveToDirectory = "C:\Patches\Exports"
        xl.DisplayAlerts = False
        wb = xl.Workbooks.Open("C:\Patches\Main_Master_VB.xlsm")
        For Each ws In wb.Sheets
            If ws.Name = "FinalStockReport_T08" Then
                ws.Copy()
                wb.Activate()
                With wb
                    .SaveAs(Filename:=("C:\Patches\Exports\" & ws.Name & "_" & DateTime.Now.ToString("dd-MMM-yyyy") & ".xlsm"))
                    .Close(False)
                End With
            End If
        Next
        xl.DisplayAlerts = True
        xl.Quit()
    End Sub

So Why it is copying all the File despite of the WS.Name ??

Notice : The Same Code Copies only One Sheet in VBA

Thanks your reply, Regards

Moheb Labib

Viewing all articles
Browse latest Browse all 15064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>