+ Reply to Thread
Results 1 to 8 of 8

Deleting rows based on a certain columns 'prefixes'?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    13

    Deleting rows based on a certain columns 'prefixes'?

    For one of my columns within an excel spreadsheet, I have department numbers that consist of five digits.
    I am hitting a roadblock in writing a macro that will delete any row containing department numbers that begin with a certain combination of 2 numbers.

    Example:

    Dept: \ Hours:
    10301 \ 134.11
    10303 \ 45.10
    10310 \ 169.05
    10311 \ 37.69
    11332 \ 4.50
    11363 \ 433.12


    Would need to delete rows containing those starting with an 11 underneath the department column, without effecting other departments or the hours column



    In addition, is there any type of macro that, after deleting those numbers starting with 11, would also delete all of the other 'prefixes' (so delete the first two numbers on all of the departments?)

    Thank you so much.


    (Sorry, I cannot provide a sample workbook, due to using this for sensitive timesheet information within my company)
    Last edited by Mgassma; 07-02-2013 at 09:24 AM.

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Deleting rows based on a certain columns 'prefixes'?

    Mgassma,

    Welcome to the Excel Forum.

    You have not given us enough information.

    1. What is the worksheet name?

    2. What column contains the title Dept:?

    3. What cell is the title Dept: in?

    4. What is the first row that contains raw data?


    I have a macro ready to go, but, I will need the answers to the above 4 questions.
    Last edited by stanleydgromjr; 07-01-2013 at 04:35 PM.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Registered User
    Join Date
    06-25-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Deleting rows based on a certain columns 'prefixes'?

    StanleyDGromJr -

    I would have been perfectly able to change any of that information from the core macro, but:

    1 - "Timecard"
    2 - The title is actually "Worked Department", and is in column A
    3 - The label is "Worked Department" is in A1
    4 - Raw data starts in the second row

  4. #4
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Deleting rows based on a certain columns 'prefixes'?

    Try this on a back up copy and see if it meets your needs. Reference to Sheet1 is to the sheet code name - change it as needed.
    This assumes values in first column (A) are always 5-digits.
    Option Explicit
    
    Sub DeleteRows()
    
        Dim vNum As Variant, vUpper As Long
        Dim lastrow As Long
        Dim C As Range
        
        vNum = Application.InputBox("Enter the two-number combination", Type:=2)
        
        If Len(vNum) > 2 Then
            MsgBox ("More than two digits were entered"), vbExclamation
            Exit Sub
        End If
        
        vNum = CLng(vNum) * 1000
        vUpper = vNum + 1 * 1000
        
        Application.ScreenUpdating = False
        
        With Sheet1
            lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
            .AutoFilterMode = False
            .Range("A1:B" & lastrow).AutoFilter Field:=1, Criteria1:=">=" & vNum, Operator:=xlAnd, Criteria2:="<" & vUpper
            .Range("A1:B" & lastrow).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            .AutoFilterMode = False
            lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
            For Each C In .Range("A2:A" & lastrow)
                C.Value = Mid(C.Value, 3, Len(C) - 2)
            Next C
        End With
        
        Application.ScreenUpdating = True
    
    End Sub
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  5. #5
    Registered User
    Join Date
    06-25-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Deleting rows based on a certain columns 'prefixes'?

    Palmetto -

    Thank you, but is there a way I can do it without having to enter the code every time? I will always be deleting those that don't start with a 10.
    Last edited by Mgassma; 07-02-2013 at 08:52 AM.

  6. #6
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Deleting rows based on a certain columns 'prefixes'?

    Here is the code revised, no input box, delete all rows that don't start with "10" and strips first two digits from department.

    Option Explicit
    
    Sub DeleteRows2()
    
        Dim lastrow As Long
        Dim c As Range
        
        Application.ScreenUpdating = False
        
        With Sheet1
            lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
            .AutoFilterMode = False
            .Range("A1:B" & lastrow).AutoFilter field:=1, Criteria1:=">10999"
            .Range("A1:B" & lastrow).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            .AutoFilterMode = False
            lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
            For Each c In .Range("A2:A" & lastrow)
                c.Value = Mid(c.Value, 3, Len(c) - 2)
            Next c
        End With
        
        Application.ScreenUpdating = True
        
        End Sub
    Last edited by Palmetto; 07-02-2013 at 09:23 AM.

  7. #7
    Registered User
    Join Date
    06-25-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Deleting rows based on a certain columns 'prefixes'?

    Nevermind, I've solved my problem. I have switched some steps around, used a code that changes the applicable data from the original 5 digit codes to the four digit codes that are needed, and ended up writing a macro that just deleted the rows containing numbers in the proper column that were greater than four digits.

    Thank you for all of your help, however.

  8. #8
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Deleting rows based on a certain columns 'prefixes'?

    Mgassma,

    If you had given us all the information we needed from the beginning, your request would have been solved the first time around.

    Thanks for the information.

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub DeleteRowsAndPrefixes()
    ' stanleydgromjr, 07/01/2013
    ' http://www.excelforum.com/excel-programming-vba-macros/935629-deleting-rows-based-on-a-certain-columns-prefixes.html
    Dim r As Long, lr As Long
    Application.ScreenUpdating = False
    With Sheets("Timecard")
      lr = .Cells(Rows.Count, 1).End(xlUp).Row
      For r = lr To 2 Step -1
        If Left(.Cells(r, 1), 2) = 11 Then
          .Rows(r).Delete
        Else
          .Cells(r, 1) = Right(.Cells(r, 1), 3)
        End If
      Next r
    End With
    Application.ScreenUpdating = True
    End Sub
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the DeleteRowsAndPrefixes macro.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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