Results 1 to 10 of 10

Thread: Delete Excel Sheets With Name Criteria

  1. #1

    Thread Starter
    Banned
    Join Date
    Apr 2020
    Location
    https://t.me/pump_upp
    Posts
    61

    Delete Excel Sheets With Name Criteria

    Dear All

    I have a code to delete excel sheets having a common Criteria in their Names Like T10, T12, T16 and so on
    I want the code to delete these sheets, the code is working on VBA but it does not on vb.net ??

    My Code is
    g
    Code:
     Private Sub DeleteTSheets()
            Dim xl As New Excel.Application
            Dim wb As Excel.Workbook
            Dim ws as Excel.Worksheet
            wb = xl.Workbooks.Open("C:\Patches\Main_Final.xlsm")
            For Each ws in wb.sheets
               If InStr(1, wb.Sheets(i).Name, "T") Then
                  If len(ws.name)=3 then
                       ws.delete()
                  End If
               End If
            Next
           End Sub
    So, What is wrong exactly?

    Thanks, Regards

    Moheb Labib

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Delete Excel Sheets With Name Criteria

    I doubt (even in VBA) that it will delete the Sheet while you are searching in it.

    break it up into more actions
    a) search the folder for excel workbooks and return taht to a listbox
    b) loop with the File name and return each sheet
    c) perform your search with that sheet name and return the cell Range where it was found
    d) then decide if you really want to delete that sheet

    here a start to get the sheet names
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            GetExcelSheetNames("E:\A9.xlsx")
        End Sub
    
        Private Sub GetExcelSheetNames(ByVal fileName As String)
            Dim strconn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
                  fileName & ";Extended Properties=""Excel 12.0 Xml;"""
            Dim conn As New OleDb.OleDbConnection(strconn)
    
            conn.Open()
    
            Dim dtSheets As DataTable =
                      conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim listSheet As New List(Of String)
            Dim drSheet As DataRow
    
            For Each drSheet In dtSheets.Rows
                listSheet.Add(drSheet("TABLE_NAME").ToString())
            Next
    
            ListBox1.DataSource = listSheet.ToArray
            conn.Close()
    
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Delete Excel Sheets With Name Criteria

    Hello,

    The command InStr is misused. The result is the position of what your are looking for. In your case the letter T at the first place. the string to use is ws.name not
    wb.Sheets(i).Name as you already loop through the sheets with the For Each.
    Code:
     Dim xl As New Excel.Application
            Dim wb As Excel.Workbook
            Dim ws As Excel.Worksheet
            wb = xl.Workbooks.Open("C:\Patches\Main_Final.xlsm")
            For Each ws In wb.Sheets
                If InStr(1, ws.Name, "T") = 1 Then
                    If Len(ws.Name) = 3 Then
                        ws.Delete()
                    End If
                End If
            Next

    I Tested it , it's working
    regards

    PS : seems, I am slow to answer
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Delete Excel Sheets With Name Criteria

    Quote Originally Posted by Delaney View Post

    I Tested it , it's working
    regards

    PS : seems, I am slow to answer
    Hi Delany,
    did it really delete the sheet while you were searching for a value ?
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Delete Excel Sheets With Name Criteria

    No, It deletes the sheet starting by T (and with 3 characters) when the workbook is closed. Th OP didn't precise that it was while searching for a value . I just corrected the code he put.
    Last edited by Delaney; Jul 4th, 2020 at 11:32 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Delete Excel Sheets With Name Criteria

    I thought that
    I think in VBA you have to put a EXIT For
    to stop the Loop and then you can Delete, but I'm not sure, and to lazy to test that
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Delete Excel Sheets With Name Criteria

    if the workbook is already open, the code would be a bit different as you should not create a new instance. You would use
    Code:
     Dim XlApp As Object = CreateObject("excel.application")
    And you will have to save the workbook with a new name but the new workbook won't have the deleted sheets
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8

    Thread Starter
    Banned
    Join Date
    Apr 2020
    Location
    https://t.me/pump_upp
    Posts
    61

    Re: Delete Excel Sheets With Name Criteria

    Dear Delaney
    Thanks your support, However the code is working as vba on excel, I want it to work on VB.net, it seems to me that the excel file may be opened in the background or read only and it won't let the code to work on VB.net, I know that it is working on VBA
    Thanks, Regards

  9. #9

    Thread Starter
    Banned
    Join Date
    Apr 2020
    Location
    https://t.me/pump_upp
    Posts
    61

    Re: Delete Excel Sheets With Name Criteria

    Dear All,

    I found The Solution, The Code is correct but it is worked only when I removed all macros from the excel sheet so what is this means??

    The Correct Code is

    Code:
     Private Sub DeleteTSheets()
            Dim xl As New Excel.Application
            Dim wb As Excel.Workbook
            Dim ws as Excel.Worksheet
            wb = xl.Workbooks.Open("C:\Patches\Main_Final.xlsm")
            For Each ws in wb.sheets
               If InStr(1, ws.Name, "T") Then
                  If len(ws.name)=3 then
                       ws.delete()
                  End If
               End If
            Next
           wb.Close(Savechanges:=vbTrue)
           End Sub

  10. #10
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Delete Excel Sheets With Name Criteria

    I wonder.. if you have Option Strict= On this should raise an error as it does for me:

    Code:
    If InStr(1, ws.Name, "T") Then
    or I missed something
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width