+ Reply to Thread
Results 1 to 6 of 6

Macro - Delete Rows of a Certain Letter Case

Hybrid View

  1. #1
    Registered User
    Join Date
    08-05-2006
    Posts
    14

    Macro - Delete Rows of a Certain Letter Case

    Hi, I have some data in Excel that follows a pattern similar to the following:

       |                A                 |             B           |          C      |
    1 | #1 - AAAAAAA - AAAA
    2 | Abcdef
    3 |
    4 | #2 - BBBBBBB - BBBB
    5 | Bcdefg
    6 |
    7 | #3 - CCCCCC - CCCC
    8 | Cdefgh
    What I want in this example is for the rows that are in "proper" case (or whatever you call a capital followed by lowecase letters), ie A2, A5, A8, to be deleted by a macro.

    Does anyone think this could be done? I'm not really sure if Excel/VBA can differentiate between cases.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    Try this.
    Sub x()
    
    Dim RegEx As Object, RegMatchCollection As Object, RegMatch As Object
    Dim rng As Range, OutPutStr As String
    
    Set RegEx = CreateObject("vbscript.regexp")
    
    With RegEx
        .Global = True
        .Pattern = "^[A-Z][a-z]+"
    End With
    
    For Each rng In ActiveSheet.Range("A1:A6") ' amend to suit
        OutPutStr = ""
        Set RegMatchCollection = RegEx.Execute(rng.Value)
        For Each RegMatch In RegMatchCollection
            OutPutStr = OutPutStr & RegMatch
        Next
        If OutPutStr <> "" Then rng.EntireRow.Delete
    Next
    
    End Sub

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Here is another macro that you could also try

    Sub DeleteCells()
       Dim lRow As Long
       
       For lRow = Cells(Rows.Count, "a").End(xlUp).Row To 1 Step -1
          With Cells(lRow, "a")
             If .Value = Application.Proper(.Value) Then
                If .Value <> "" Then
                   .Delete Shift:=xlUp
                End If
             End If
          End With
       Next lRow
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    And another way, I prefer to find all the cells then do one delete

    Option Explicit
    
    Sub deleteRows()
        Dim cl     As Range
        Dim rng    As Range
        Dim R      As Long
        Dim X      As Long
    
        R = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        For X = 1 To R
            If Not IsEmpty(Cells(X, 1)) And Cells(X, 1).Value = Application.Proper(Cells(X, 1).Value) Then
                If rng Is Nothing Then
                    Set rng = Cells(X, 1)
                Else: Set rng = Union(rng, Cells(X, 1))
                End If
            End If
        Next X
        rng.EntireRow.Delete
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    08-05-2006
    Posts
    14
    Yeah Roy, I was thinking the find and delete option could be worth trying.

    Thanks very much guys, I'll give them all a go when I get a chance. Great work.

  6. #6
    Registered User
    Join Date
    08-05-2006
    Posts
    14
    Cheers, gave those a go and they all worked like a charm.

+ 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