+ Reply to Thread
Results 1 to 2 of 2

Future Proof Code for new Column entry

Hybrid View

  1. #1
    Registered User
    Join Date
    04-18-2017
    Location
    LONDON
    MS-Off Ver
    2013
    Posts
    19

    Future Proof Code for new Column entry

    Hi All

    I have some code that is currently working for me as an archive function. If values in a cell contain certain criteria it copies the whole row to another sheet and deletes. However, issue I am facing is that if I add a new column into the sheet, I have to up version the code each time. Is there a clean way of future proofing the code so that it works to find the criteria based on the column name in a table??

    The sheet I am writing the code for has a table called Main Table and there are two columbs one called 'Live' and one called BCR Demand Status

    Essentially I'd like to be able to insert a new column and instead of the code looking for the value column 26, it will alway look for the values in Column BCR demand status within the table.

    Thanks

    Sub CutCopy()
    If MsgBox("Are you sure you want to move Live or Rejected demand to Archive", vbYesNo + Question) = vbYes Then
    Worksheets("Demand Capture").Unprotect
    Worksheets("Archive").Unprotect
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lastrow As Long, i As Long, j As Long
    Set ws1 = Sheets("Demand Capture")
    Set ws2 = Sheets("Archive")
    lastrow = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    For i = 4 To lastrow
        If ws1.Cells(i, 1).Value = "Y" Or ws1.Cells(i, 26).Value = "Rejected" Then
            ws1.Rows(i).Copy
            j = 2
            Do Until IsEmpty(ws2.Cells(j, 1))
                j = j + 1
            Loop
            ws2.Cells(j, 1).PasteSpecial (xlPasteAll)
            ws1.Rows(i).EntireRow.Delete
            i = i - 1
            lastrow = lastrow - 1
        End If
        Next i
        Worksheets("Demand Capture").Protect Contents:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, DrawingObjects:=True
        Worksheets("Archive").Protect Contents:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, DrawingObjects:=True
        Worksheets("Demand Capture").Activate
        ActiveWindow.Zoom = 85
        MsgBox "Archive Complete"
        Else
    Exit Sub
    End If
    End Sub

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,640

    Re: Future Proof Code for new Column entry

    Option Explicit
    
    Sub CutCopy()
        If MsgBox("Are you sure you want to move Live or Rejected demand to Archive", vbYesNo, "+ Question") = vbYes Then
            Dim DEMAND As Worksheet
            Dim ARCHIVE As Worksheet
            Dim lastrow As Long, i As Long, j As Long
            
            Dim BCR As Range 
    
            Set DEMAND = Sheets("Demand Capture")
            Set ARCHIVE = Sheets("Archive")
            DEMAND.Unprotect
            ARCHIVE.Unprotect
            
            With DEMAND
                lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
                
             Set BCR = .Range("1:1").Find("BCR")
                
                For i = 4 To lastrow
                    If .Cells(i, 1).Value = "Y" Or .Cells(i, BCR.Column).Value = "Rejected" Then
                        .Rows(i).Copy
                        j = 2
                        Do Until IsEmpty(ARCHIVE.Cells(j, 1))
                            j = j + 1
                        Loop
                        ARCHIVE.Cells(j, 1).PasteSpecial (xlPasteAll)
                        .Rows(i).EntireRow.Delete
                        i = i - 1
                        lastrow = lastrow - 1
                    End If
                Next i
            End With    'DEMAND
            
            DEMAND.Protect Contents:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, DrawingObjects:=True
            ARCHIVE.Protect Contents:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, DrawingObjects:=True
            DEMAND.Activate
            ActiveWindow.Zoom = 85
            MsgBox "Archive Complete"
        Else
            Exit Sub
        End If
    End Sub
    Ben Van Johnson

+ 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. [SOLVED] Looking to proof some code I added.
    By Nitro2481 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2016, 06:55 AM
  2. [SOLVED] Restrict Entry into excel for dates more than 2 weeks in future
    By xxxyyyy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-03-2016, 12:29 AM
  3. Proof that a column is sorted
    By ek56 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-15-2015, 07:08 AM
  4. Future Proof - Expansion Buttons
    By batchy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-30-2015, 10:00 AM
  5. [SOLVED] What is the ideal, 100% full proof, way to find the last row or last column?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-17-2013, 09:56 AM
  6. Beginner needs code to be proof read (there ARE problems)
    By dcase in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-29-2006, 11:20 AM
  7. space left for future entry = #DIV/0!
    By clooney in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 05-24-2006, 09:46 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