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

Threaded View

Hillster VBA code for 2 buttons that... 11-02-2019, 10:55 PM
BanginMyHeadOnMyDesk Re: VBA code for 2 buttons... 11-03-2019, 05:25 AM
Hillster Re: VBA code for 2 buttons... 11-03-2019, 08:07 AM
BanginMyHeadOnMyDesk Re: VBA code for 2 buttons... 11-03-2019, 09:37 AM
LJMetzger Re: VBA code for 2 buttons... 11-03-2019, 04:19 PM
Hillster Re: VBA code for 2 buttons... 11-03-2019, 09:41 PM
LJMetzger Re: VBA code for 2 buttons... 11-04-2019, 10:26 AM
Hillster Re: VBA code for 2 buttons... 11-03-2019, 09:47 PM
Logit Re: VBA code for 2 buttons... 11-03-2019, 11:53 PM
Hillster Re: VBA code for 2 buttons... 11-04-2019, 07:44 AM
Logit Re: VBA code for 2 buttons... 11-04-2019, 01:19 PM
Hillster Re: VBA code for 2 buttons... 11-04-2019, 01:44 PM
Logit Re: VBA code for 2 buttons... 11-04-2019, 02:09 PM
Hillster Re: VBA code for 2 buttons... 11-04-2019, 03:15 PM
Logit Re: VBA code for 2 buttons... 11-04-2019, 06:34 PM
Hillster Re: VBA code for 2 buttons... 11-08-2019, 09:14 PM
  1. #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'.
    Option Explicit
    
    Const sTestSheetNAME = "Test Bed"
    Const sOriginalDataSheetNAME = "Sheet1"
    
    Const nFirstDataROW = 5
    
    Sub ClearOneRowAtATime()
      'This clears all data from the first row (starting at row 5) that contains data
    
      Dim myWorkbook As Workbook
      Dim myWorksheet As Worksheet
      
      Dim iCount As Long
      Dim iLastRowUsed As Long
      Dim iRow As Long
    
      'Create the Worksheet Objects
      Set myWorkbook = ThisWorkbook
      Set myWorksheet = myWorkbook.Sheets(sTestSheetNAME)
      
      'Find the Last Row Used (NOTE: A runtime error will occur if the entire sheet has no data)
      iLastRowUsed = myWorksheet.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
      'Find the First Row with Data (counts Numbers only)
      'Clear Data From that Row
      For iRow = nFirstDataROW To iLastRowUsed
      
        iCount = Application.WorksheetFunction.Count(myWorksheet.Rows(iRow))
        'Debug.Print iRow, iCount 'Output to the Immediate Window (Ctrl G in the Debugger)
        
        'If the Row contains Numbers, clear all data from the Row and Exit the loop
        If iCount > 0 Then
          myWorksheet.Rows(iRow).ClearContents
          Exit For
        End If
      
      Next iRow
    
      'Clear Object Points
      Set myWorkbook = Nothing
      Set myWorksheet = Nothing
      
    End Sub
    
    Sub RestoreOneRowAtATime()
      'This restores data from the first row (starting at from the rear) that contains no data
    
      Dim myWorkbook As Workbook
      Dim mySourceWorksheet As Worksheet
      Dim myDestinationWorksheet As Worksheet
      
      Dim r As Range
    
      Dim iCount As Long
      Dim iLastRowUsed As Long
      Dim iRow As Long
      
      'Create the Worksheet Objects
      Set myWorkbook = ThisWorkbook
      Set mySourceWorksheet = myWorkbook.Sheets(sOriginalDataSheetNAME)
      Set myDestinationWorksheet = myWorkbook.Sheets(sTestSheetNAME)
      
      'Find the Last Row Used (NOTE: A runtime error will occur if the entire sheet has no data)
      iLastRowUsed = myDestinationWorksheet.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      
      'Find the First Row with Data (counts Numbers only)
      'Copy Data From the Source Worksheet Row to the Destination Worksheet Row, then Exit the Loop
      For iRow = nFirstDataROW To iLastRowUsed
      
        iCount = Application.WorksheetFunction.Count(myDestinationWorksheet.Rows(iRow))
        'Debug.Print iRow, iCount 'Output to the Immediate Window (Ctrl G in the Debugger)
        
        'If the Row contains Numbers, Copy the 'Previous Row' Numbers to the Destination Worksheet, then Exit the loop
        If iCount > 0 Then
          myDestinationWorksheet.Rows(iRow - 1).Value = mySourceWorksheet.Rows(iRow - 1).Value
          Exit For
        End If
      
      Next iRow
        
      'Clear Object Points
      Set myWorkbook = Nothing
      Set mySourceWorksheet = Nothing
      Set myDestinationWorksheet = Nothing
    
    End Sub
    
    Sub RestoreAllData()
      'This restores all Data from the 'Original' Sheet to the 'Test Bed' Sheet
    
      Dim myWorkbook As Workbook
      Dim mySourceWorksheet As Worksheet
      Dim myDestinationWorksheet As Worksheet
      
      Dim r As Range
    
      'Create the Worksheet Objects
      Set myWorkbook = ThisWorkbook
      Set mySourceWorksheet = myWorkbook.Sheets(sOriginalDataSheetNAME)
      Set myDestinationWorksheet = myWorkbook.Sheets(sTestSheetNAME)
      
      'Copy All Data From Source WorkSheet to the Destination Worksheet
      For Each r In mySourceWorksheet.UsedRange
        myDestinationWorksheet.Range(r.Address).Value = r.Value
      Next r
        
      'Clear Object Points
      Set myWorkbook = Nothing
      Set mySourceWorksheet = Nothing
      Set myDestinationWorksheet = Nothing
    
    End Sub
    Lewis
    Attached Files Attached Files

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