+ Reply to Thread
Results 1 to 7 of 7

Macro not finding finding selected characters in strings

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,491

    Macro not finding finding selected characters in strings

    Following code should look at every cell in sheet 1 used range, move any rows where a cell contains one of these four characters: ' or ; or - or ! and delete the blank row in sheet 1.

    It's cycling through the cells correctly, but not finding the characters:

    Option Explicit
    Dim sht As Worksheet, sht2 As Worksheet
    Dim a As Long, j As Long, x As Long
    Dim sCharOK As String, s As String
    Dim r As Range, rc As Range
    
    Sub Characters()
    
    Set sht = Sheet1
    Set sht2 = Sheet2
    
        With sht2
    'Find the first free row on the Database containing data and set to memory
        a = sht2.Cells.Find("*", SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row + 1
        If a < 2 Then a = 2
        
    sht.Activate
    
    'Set the range to check
    Set r = sht.UsedRange
    
    'Set the characters to look for:
    sCharOK = " ' ; - !"
    
    'Cycle through the range and move any rows containing the characters to next free row on sheet 2
    For Each rc In r
        s = rc.Value
        For j = 1 To Len(s)
        If InStr(s, sCharOK) > 0 Then
            x = ActiveCell.Row
            Rows(ActiveCell.Row).Cut
            sht2.Range("A" & a).Insert
    'Reset the next free row and delete empty row in Sheet 1
            a = a + 1
            sht.Rows(x).Delete
            End If
        Next j
        
    Next rc
    
    End With
    
    End Sub
    Any guidance, suggestions or solutions welcome as ever

    Ochimus
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,024

    Re: Macro not finding finding selected characters in strings

    Try incorporating this change
    Dim char as String
     For j = 1 To Len(s)
            char = Mid(rc, j, 1)
            If InStr(sCharOK, char) > 0 Then

    This works....
    Option Explicit
    
    Sub Characters()
    Dim cell As Range
    Dim i As Long, nRow As Long
    Dim char As String, sCharOK As String
    Application.ScreenUpdating = False
    sCharOK = "'-;!"
    For Each cell In Sheet1.UsedRange.Offset(1, 0)
        For i = 1 To Len(cell)
            char = Mid(cell, i, 1)
            If InStr(sCharOK, char) > 0 Then
                With Sheet2
                    nRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                    Sheet1.Range("A" & cell.Row & ":C" & cell.Row).Copy
                    .Range("A" & nRow).PasteSpecial xlPasteValues
                End With
            End If
        Next i
    Next cell
    Application.ScreenUpdating = True
    End Sub
    Or perhaps as you are deleting blanks in sheet1 anyway no need to copy....
    Option Explicit
    
    Sub Characters()
    Dim cell As Range
    Dim i As Long, ii As Long, lRow As Long
    Dim char As String, sCharOK As String
    Dim Found As Boolean
    Application.ScreenUpdating = False
    sCharOK = "'-;!"
    With Sheet1
        lRow = .Cells(Rows.Count, "A").End(xlUp).Row
        For i = lRow To 2 Step -1
        Found = False
            For Each cell In .Range("A" & i & ":C" & i)
                For ii = 1 To Len(cell)
                    char = Mid(cell, ii, 1)
                    If InStr(sCharOK, char) > 0 Then Found = True
                Next ii
            Next cell
            If Found = False Then Rows(i).Delete
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 11-11-2017 at 10:53 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,491

    Re: Macro not finding finding selected characters in strings

    Sintek,

    Really grateful for the prompt response.

    The only one that seemed to work was the second suggestion, and that never deleted the relevant rows from sheet 1, so i added that to the Code.

    Option Explicit
    Dim cell As Range
    Dim i As Long, nRow As Long
    Dim char As String, sCharOK As String
    
    Sub Characters()
    
    Application.ScreenUpdating = False
    
    On Error Resume Next
    
    sCharOK = "'-;!"
    
    For Each cell In Sheet1.UsedRange.Offset(1, 0)
        For i = 1 To Len(cell)
            char = Mid(cell, i, 1)
            If InStr(sCharOK, char) > 0 Then
                With Sheet2
                nRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                Sheet1.Range("A" & cell.Row & ":C" & cell.Row).Copy
                .Range("A" & nRow).PasteSpecial xlPasteValues
                Sheet1.Rows(cell.Row).Delete
                End With
            End If
        
        Next i
    
    Next cell
    
    Application.ScreenUpdating = True
    
    End Sub
    When I ran it, the Macro missed the Hyphen in C5 and the two Hyphens in B8, but then running it again straight away it DOES pick them up, leaving just rows 2 and 6 on Sheet 1, as expected.

    Any ideas?

    Ochimus

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,301

    Re: Macro not finding finding selected characters in strings

    Quote Originally Posted by Ochimus View Post
    ... When I ran it, the Macro missed the Hyphen in C5 and the two Hyphens in B8, but then running it again straight away it DOES pick them up, leaving just rows 2 and 6 on Sheet 1, as expected.
    1. 'Exit For' is missing:
    2. Deleting a row not here (red) because you are changing the area you are working on (mark the cells and run delete at the end of the macro):
                If InStr(sCharOK, char) > 0 Then
                    With Sheet2
                        nRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                        ...
                        Sheet1.Rows(cell.Row).Delete
                    End With
                    Exit For
                End If
    3. code below:
        Sheet1.Range("A" & cell.Row & ":C" & cell.Row).Copy
        .Range("A" & nRow).PasteSpecial xlPasteValues
    you can change to:
        .Range("A" & nRow & ":C" & nRow).Value = Sheet1.Range("A" & cell.Row & ":C" & cell.Row).Value

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,994

    Re: Macro not finding finding selected characters in strings

    Sub Macro1()
        Dim SOURCE      As Variant, _
            DEST        As Worksheet, _
            Found       As Variant, _
            DestRow     As Long, _
            FoundRow    As Long, _
            Chars       As Variant, _
            TestChar    As Variant
            
        Set SOURCE = Sheets("sheet1")
        Set DEST = Sheets("sheet2")
        Chars = Array("-", ";", "!", "'")
        
        DEST.Range("a1").Resize(columnsize:=3).Value = SOURCE.Range("A1:c1").Value
        DestRow = 2
        
        For Each TestChar In Chars
            Set Found = Nothing
            With SOURCE.UsedRange
                Set Found = .Find(TestChar)
                If Not Found Is Nothing Then
                    
                    Do
                        Debug.Print TestChar, Found.Address
                        
                        DEST.Range("A" & DestRow).Resize(columnsize:=3).Value = SOURCE.Cells(Found.Row, "A").Resize(columnsize:=3).Value
                        DestRow = DestRow + 1
                        Range("A" & Found.Row).Resize(columnsize:=3).Delete Shift:=xlUp
                        Range("A1").Select
                        Set Found = .Find(TestChar)
                    Loop While Not Found Is Nothing
                End If
            End With
        Next TestChar
    End Sub
    Ben Van Johnson

  6. #6
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,491

    Re: Macro not finding finding selected characters in strings

    Well, it's enormous thanks to everyone who responded.

    Trust Excel to provide four totally different solutions to the same problem! Tempting to try each one over a few thousand records and see whether there is any significant difference in speed between the different approaches.

    Ochimus

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,024

    Re: Macro not finding finding selected characters in strings

    When deleting rows one always starts from Bottom to Top otherwise you will be skipping rows...
    Option Explicit
    
    Sub Characters()
    Dim cell As Range
    Dim i As Long, ii As Long, lRow As Long, nRow As Long
    Dim char As String, sCharOK As String
    Application.ScreenUpdating = False
    sCharOK = "'-;!"
    With Sheet1
        lRow = .Cells(Rows.Count, "A").End(xlUp).Row
        For i = lRow To 2 Step -1
            For Each cell In .Range("A" & i & ":C" & i)
                For ii = 1 To Len(cell)
                    char = Mid(cell, ii, 1)
                    If InStr(sCharOK, char) > 0 Then
                        With Sheet2
                            nRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                            Sheet1.Range("A" & cell.Row & ":C" & cell.Row).Copy
                            .Range("A" & nRow).PasteSpecial xlPasteValues
                        End With
                        Rows(i).Delete
                        GoTo nxt
                    End If
                Next ii
            Next cell
    nxt:
        Next i
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub
    Thank you for taking the time to Add to reputation

+ 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. Finding two sub-strings within a string
    By pablowilks in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2015, 03:05 PM
  2. Finding two sub-strings within a string
    By pablowilks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-22-2015, 10:00 AM
  3. Replies: 9
    Last Post: 12-21-2012, 04:18 AM
  4. Finding exact words i strings
    By Flabbergaster in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-16-2012, 01:48 PM
  5. Error 91 while finding strings
    By spammy2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2012, 01:33 PM
  6. Macro for finding out all non-keyboard characters and symbols in a sheet
    By Merry in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2008, 04:26 PM
  7. Finding differences in strings
    By tchezick in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-22-2005, 11:55 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