Results 1 to 6 of 6
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371

    Adjusting Code To Delete Worksheets

    Hi Guy's, ranman kindly gave me this option to add data from Access to excel and generates worksheets A to Z and adds the data.

    This works great, the question I have is if I added a new contact that starts with C for example:

    Can i adapt this code to delete worksheet C then replace after update once postcode is added ?

    I am guessing where Currentdb.QueryDefs.Append qdf, is it a matter of running similar code before hand and changing .Append to .Delete ?

    assuming this will delete all worksheets from A to Z ? it wouldn't matter if we done that and replace all again, in fact guy's, it maybe wise to delete all works sheets that start A,B,C to Z and replace because if another user adds a record starting T and I have added starting with C then more than one sheet requires update ?



    How can I adapt this to delete the sheets A-Z then run the following code to replace them ?

    Code:
    Dim sSql As String, srcPath As String, srcFile As StringDim vChr
    Dim i As Integer
    Dim qdf As QueryDef
    Const kQRY = "qsNames2Export"
    Dim vFile, vLtr
    
    
    
    
    On Error Resume Next
    
    
    srcPath = "T:\DMT Ltd\XL Files\"
    srcFile = "New Items.xlsx"
    
    
    vFile = srcPath & srcFile
    
    
    
    
     sSql = "select * from tblDealers where left(ucase([Name]),1)<='A'"
    Set qdf = New QueryDef
    qdf.Sql = sSql
    qdf.Name = kQRY
    CurrentDb.QueryDefs.Append qdf
    
    
    
    
    For i = 65 To 90
         vLtr = Chr(i)
         If i = 65 Then   'A or less
            sSql = "select tblDealers.Name, tblDealers.Town, tblDealers.Postcode from tblDealers where left(ucase([Name]),1)<='" & vLtr & "' ORDER BY tblDealers.Name, tblDealers.Name;"
         Else
            sSql = "select tblDealers.Name, tblDealers.Town, tblDealers.Postcode from tblDealers where left(ucase([Name]),1)='" & vLtr & "' ORDER BY tblDealers.Name, tblDealers.Name;"
         End If
         
         Set qdf = CurrentDb.QueryDefs(kQRY)
         qdf.Sql = sSql
         qdf.Close
         
         DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, kQRY, vFile, True, vLtr
    Next
    Set qdf = Nothing

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    No, changing .Append to .Delete will not delete worksheets.

    If there are no other worksheets, maybe easier to delete workbook and create a new one.

    If Dir(vFile) <> "" Then Kill vFile
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hu June7, yes there are 3 sheets Sheet1, Sheet2, Sheet3 then afterwards it's A-Z

    the code that works does generate the sheets after any sheet that already exists in that workbook

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    And there is data on Sheets 1, 2, 3?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    If you don't want to recreate a new file each time as suggested by June7 then you need to use automation to delete the sheets. Here is an example of how to do just that:
    https://powerspreadsheets.com/excel-vba-delete-sheet/

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    H Guy's thank you very much will follow up once I can make adjustments

    Kindest

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 06-27-2018, 08:07 AM
  2. Replies: 7
    Last Post: 09-16-2017, 04:01 PM
  3. Replies: 2
    Last Post: 08-10-2015, 11:53 AM
  4. It wants me to delete all my code!
    By IncidentalProgrammer in forum Programming
    Replies: 3
    Last Post: 10-28-2014, 08:55 AM
  5. Code to delete the blank rows
    By drunkenneo in forum Programming
    Replies: 3
    Last Post: 02-17-2014, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums