+ Reply to Thread
Results 1 to 16 of 16

VBA code for 2 buttons that can clear cells in a row then second button to bring data back

  1. #1
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    VBA code for 2 buttons that can clear cells in a row then second button to bring data back

    I need help with 2 form control buttons that i need to clear data out of rows and then click another button to bring the same data back into the rows. I need to perform some back testing and was wondering if there was a VBA code that can be applied to two buttons. Button one, "Clear" and button two, "Retrieve" starting with row 5. I want to be able to remove 1 row at a time. If i click the "clear" button 5 times, 5 rows are gone then I click the Retrieve button 5 times and all the original data comes back and is in the same state as before. On thing I would like to make sure of is when exiting the workbook, all data is retrieved and back on the sheet prior to exit as I want the data to ultimately remain on the sheet. I have attached a work sheet with some example data. Any assistance would be much appreciated.
    Attached Files Attached Files
    Last edited by Hillster; 11-02-2019 at 11:04 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    A few questions on this...
    1) When you click Clear the first time will it remove the first column of data, second time the second one etc.?
    2) Clicking Retrieve should bring them back in the reverse order of how they were removed?
    3) Do they need to be removed or could they just be hidden?

    Beth.

  3. #3
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Hey BanginmyHeadOnMyDesk,

    1) When you click Clear the first time will it remove the first column of data, second time the second one etc.?

    Answer: Correct, 1st click removes Row 5 Data, second click removes 6 row Data, etc....

    2) Clicking Retrieve should bring them back in the reverse order of how they were removed?

    Answer: Correct, so if i click Clear 99 times, i am going to have to click Retrieve 99 times. That is the best part of the whole process. exactly what i want to have to do... BUT.... Upon exiting the workbook, the VBA will retrieve all the data back to its original state.

    3) Do they need to be removed or could they just be hidden?

    Answer: So the possible tricky part.... I would say Removed or Replaced actually.

    Explanation: I have other cells referencing Row 5.... Row 5 is the "Key" Row and the cells that are referencing it are in other sheets so..... in all reality, when the user clicks the "Clear" button, VBA will retrieve all the data starting at Row 6 and below then paste it over the top of Row 5's data... The result after one Click of the clear button: Row 6 data is now in Row 5. All the data gets bumped up one row and the original data that once was in Row 5 is now gone UNTIL the user clicks the "Retrieve" button, then VBA pushes all of the data back down by one row and the original Row 5 data is back again. So there always has to be data in Row 5..... Make sense? Then as a reminder, when the user exits the workbook, VBA will ensure all data is back to it's original state as if the "Clear" button was never clicked
    Last edited by Hillster; 11-03-2019 at 08:18 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    I think it would be helpful to see this in context. It may be that it's easier to hide the row and amend the formulas than to get rid of a row and move the remaining data just to keep the formulas working. Would you be able to attach a copy of the full file? If there is sensitive information included then desensitize it first.

    The reason I'm leaning toward hiding the data is to retrieve it, either at the click of the button or when closing the file, means you have to store it somewhere rather than just deleting it.

    Beth.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    I took a slightly different approach to your problem. In cases like yours, I usually use a 'Shadow Sheet' containing a copy of the original data and do all the testing on the 'Shadow' Sheet. When testing is complete, I delete the Shadow Sheet and use the Original Sheet in production. See the code that follows which is included in the Sample Workbook. All activity takes place on Sheet 'Test Bed'.
    Please Login or Register  to view this content.
    Lewis
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Thanks for the fast responses guys.... LJMetzger, I am getting a very interesting result with your copy of the work sheet with VBA in it... when I pasted your code into my master Workbook, then added the form control buttons, click them. it keeps opening your copy of the example work sheet you posted here. It does it even if i exit out of your example work sheet.... I don't even know how that could possibly happen. Lol I inserted a Module in my workbook, copy / pasted your code into it. clicked the "Clear" button and row 5 data remains in the worksheet but your example copy pops up with all of Row 5 data removed... Not a usable function but cool to see. Also tried clicking the "Restore Original Data" button... Not a good idea as it locked up Excel for at least 15 minutes until I hit ctrl Alt Delete to End Task... The concept is on point, just does not work in my workbook. Seems awesome in the example though, nice work!!
    Last edited by Hillster; 11-03-2019 at 09:43 PM.

  7. #7
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Thanks BanginMyHeadOnMyDesk, I do have data that i can't desensitize and the file size of my workbook is way to large to attempt uploading anyway....The thing i worry about with "hiding" the data in rows is the other sheets have alot of cells referencing the ones we might apply a "hide" function to but if the data is still inside the cells of row 5 wouldn't the cells on other sheets still see them and continue referencing them? Make sense?

  8. #8
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,407

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    .
    I believe this is what you were seeking :

    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Thanks Logit, that is exactly what I want but the code is not working when I pasted it into a module in my main workbook. I do not have any sheets named "sheet1" or "sheet2" which i see in your code so I switched to the actual name of my sheet that needs the vba function, ("Input Page"). As expected it returned all kinds of Debug errors which I tried to resolve. No success.
    I noticed part of the code appears to reference sheet2? is see sheet2 of your example has no data in it... why would vba be looking at a sheet with no data?
    Last edited by Hillster; 11-04-2019 at 07:51 AM.

  10. #10
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Hillster wrote: < when I pasted your code into my master Workbook, then added the form control buttons, click them. it keeps opening your copy of the example work sheet>

    The macros associated with the Control Buttons are still pointing to my file - a powerful feature of Excel when you want that feature.

    Since you want the macros to point to your file, you have to reassign the buttons to point to the Macros of the same name in your file:
    a. Right Click the Command Button
    b. Select 'Assign Macro' and select the appropriate Macro, then select 'OK'.

    Lewis

  11. #11
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,407

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Thanks Logit, that is exactly what I want but the code is not working when I pasted it into a module in my main workbook. I do not have any sheets named "sheet1" or "sheet2" which i see in your code so I switched to the actual name of my sheet that needs the vba function, ("Input Page"). As expected it returned all kinds of Debug errors which I tried to resolve. No success.
    I noticed part of the code appears to reference sheet2? is see sheet2 of your example has no data in it... why would vba be looking at a sheet with no data?
    You can edit the code to match your sheet names. If you are having difficulty getting that to function properly ... let me know what your sheet names are and I can edit it for you. If I correctly understand, the main sheet with your data is called "input Page". Is that correct ?

    The reason the code is looking for data in Sheet2 is because when you remove a line of data from your Input Page ... it must be placed somewhere so it can be retrieved later to be placed back on Input Page. The sheet for temporary placement of the data can be any sheet. If you are presently using Sheet2 for something else ... the project can utilize a different sheet .... it can be named "Temp Data" or anything else .... and perhaps even hide it to prevent accidentally using the sheet for something else.

    Let me know.

  12. #12
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Logit. you are correct, the sheet i am trying to control the data by using VBA is named "Input Page". I have created a "sheet2" sheet so your code can utilize the temporary data... One item i did discover is the data across the "Input Page" / sheet is getting scrambled up pretty badly when the vba runs... I wonder if we could try instead of removing the entire row 5, can vba remove only cells B5:G5, everything else in the vba code remains the same as far as creating the "Temp Sheet", the use of the clear and retrieve buttons etc... is that possible? and if you don't mind changing the "sheet1" reference to "Input Page", that would be awesome!!! Let me know your thoughts.

  13. #13
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,407

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    .
    The code has been edited to what I believe you are seeking.

    Please Login or Register  to view this content.

    Workbook attached.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    I think it's still removing the entire row 5 data "Sheets("Input Page").Range("D2:L2").Copy" instead i think it should be referencing "C5:F5" that is the actual row my data is on my workbook. If we could get that to work it would be great. The next trick will be me trying to actually get that code into my workbook and have it actually work properly. I don't even know where I am supposed to put the code when it gets to my workbook... I see where it is on your Example workbook...I'm sure it will be different on mine. I sure appreciate your help with this! Thank you!

  15. #15
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,407

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    .
    Any chance you could share your workbook ? It would certainly cut down on all the back and forth.

    If you need to you can send it to my private email. Let me know and I'll send you a private message.

  16. #16
    Forum Contributor
    Join Date
    09-09-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    176

    Re: VBA code for 2 buttons that can clear cells in a row then second button to bring data

    Hey Logit, i sent you a follow up email. Did you receive it?

+ 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. Replies: 1
    Last Post: 12-12-2017, 12:49 PM
  2. [SOLVED] Help with code to creat Reset button to clear cells and copy back the original formula
    By rv02 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-30-2015, 02:25 PM
  3. Array Formula Lookup - Bring back Lowest Date + Bring Back Cell Location
    By Matt1998 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-04-2014, 12:08 PM
  4. Replies: 3
    Last Post: 06-05-2012, 04:36 AM
  5. Replies: 2
    Last Post: 02-09-2009, 05:07 AM
  6. Create a button to clear data in certain cells
    By headbanger51 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-07-2008, 03:18 AM

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