+ Reply to Thread
Results 1 to 10 of 10

How to write VBA code to clear a table but preserve table and 1 empty row

  1. #1
    Registered User
    Join Date
    06-20-2013
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    55

    How to write VBA code to clear a table but preserve table and 1 empty row

    Worksheet 1 is called "Instructions:
    Worksheet 2 is called "Copy the TXT File Data Here"
    The data is formatted as a Table and the table is named "tbl_OpenItems"
    The data and table are located on worksheet 2
    Within the table "tbl_OpenItems"...
    Row 1 is the header row
    Row 2-n has data
    There are 15 columns (so A-O)
    There are no formulas to preserve
    When a new users comes into the file, I want them to be able to hit a button that will allow them to clear the old contents which will then allow them to copy in their new data (which will always have a different # of rows)
    When the button is hit, I want all data in the table, but NOT the headers to be cleared.
    I "think" but I could be wrong, that I need to preserve Row 2 (all blank) so that the User copying in their new data has Cell A2 as their starting point to hit CTRL-V to paste in their data.
    The button will be located on Tab 1 called "Instructions" (I may put another version of this button on the worksheet with the table as well, but I'll cross that road later if I do)
    The table (tbl_OpenItems) is located on Tab 2 called "Copy the TXT File Data Here"

    My apologies as I'm still learning VBA, Macros, etc so I will need precise instructions on how to write this, how and where to save it (i.e. I don't understand when to use or not use Modules), etc. Every detail will be important so I can follow exactly how to implement this. If the code has a Macro name (not sure I'm saying that correctly, but hopefully you know what I mean), then I know how to Assign a button to the Macro so the user can simply click the button to execute the VBA code.

    Additional nice to haves (these would be really nice):
    - When the button is clicked, a warning should say "Are you sure you want to clear the OpenItems table?" Yes No
    - Clicking No pops a new message saying "The operation was cancelled as you requested. Click OK to close and continue where you left off." OK
    - Clicking Yes would clear the table and a pop-up message would occur saying "You have cleared the table. Click OK to close this window". OK
    - User should be taken to Tab 1 ("Instructions", cell A119) after clicking OK to either the No or Yes pop-up message.

  2. #2
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    Hello Kwoltman,


    The following code, assigned to a button, should do the task for you:-


    Please Login or Register  to view this content.
    Change the table name to suit.

    I've attached a sample workbook to show you how this works. Click on the "Delete" button to execute the code.

    First, let us know if this suits you then we can do the nitty gritty stuff with helping you to implement it.

    I hope that this helps.

    Cheerio,
    vcoolio.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    06-20-2013
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    That is what I'm looking for. Now I just need to figure out where it goes (Module?) and how do make that happen.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    Create a Standard Module and paste the code in it.

    Then insert a button from the Forms Collection and assign the macro to it.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Registered User
    Join Date
    06-20-2013
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    Thank you both!
    The only thing that isn't working is the ActiveSheet part (I believe that's the issue)
    The button is located on the worksheet called "Instructions"
    The Table being cleared is on another worksheet called "Copy the TXT File Data Here"
    So when I run it, it bugs out. Debugging highlights this line...

    With ActiveSheet.ListObjects("tbl_OpenItem").DataBodyRange '----> Change table name to suit.

    I believe I have to tell it the worksheet name and then I still want to return the user to Cell A119 on the "Instructions" worksheet.

    Sorry, one more thing...and can we add a "Cancel" option if the following Message boxes occur?

    Are you sure you want to delete the current data? (If YES is answered)
    Then the next message box is "Data will be cleared. Click OK to continue." OK is the only option here and I think it would help to have a CANCEL option since I'm providing a warning message I feel like I should give the user 1 last chance to stop/cancel the delete.

    This is the current code:

    Sub ClearOpenItemTable()

    Dim lr As Long: lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

    With ActiveSheet.ListObjects("tbl_OpenItem").DataBodyRange '----> Change table name to suit.
    If MsgBox("Are you sure you want to delete the current data?", vbYesNo, "WARNING") = vbNo Then
    MsgBox "Operation has been cancelled. Click OK to continue.", vbExclamation, "Operation cancelled"
    Else
    MsgBox "Data will be cleared. Click OK to continue.", vbExclamation, "WARNING"
    Range("A2:O" & lr).Delete
    End If
    End With

    End Sub
    Last edited by kwoltman; 03-28-2019 at 09:46 AM.

  6. #6
    Registered User
    Join Date
    06-20-2013
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    See above (please ignore this message)

  7. #7
    Registered User
    Join Date
    06-20-2013
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    bakerman2...just need a little more help and then I think I'm done.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    Please Login or Register  to view this content.
    Last edited by bakerman2; 03-28-2019 at 11:31 AM.

  9. #9
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    Hello Kwoltman,

    See if the following does all that you need:-


    Please Login or Register  to view this content.
    I've attached an updated sample for you to play with.

    Cheerio,
    vcoolio.
    Attached Files Attached Files

  10. #10
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: How to write VBA code to clear a table but preserve table and 1 empty row

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    HTH
    Regards, Jeff

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Write back to table source from a resorted table on another worksheet
    By fuadramsey in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-17-2018, 07:42 PM
  2. Replies: 5
    Last Post: 03-19-2018, 02:42 PM
  3. How to write VBA code to check if a database table containing certain records?
    By VAer in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-02-2017, 04:44 PM
  4. Replies: 46
    Last Post: 08-27-2015, 01:51 AM
  5. Change macro to write to next empty row of table?
    By humboldtguy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2014, 03:17 AM
  6. [SOLVED] How do I write code to preserve variables values for future use.
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2005, 12:25 PM
  7. Pivot table to launch Macro- preserve format under table options
    By Darin Kramer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2005, 02:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1