+ Reply to Thread
Results 1 to 7 of 7

Delete other values except "A" and "B" and "C"

Hybrid View

  1. #1
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Delete other values except "A" and "B" and "C"

    Greetings,
    I have following code in the enclosed file.
    Sub Macro1()
    Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Replace What:=">", Replacement:="", lookat:=xlPart
    Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Replace What:="BT", Replacement:="", lookat:=xlPart
    Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Replace What:="p", Replacement:="", lookat:=xlPart
    Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Replace What:="t", Replacement:="", lookat:=xlPart
    Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Replace What:=" ", Replacement:="", lookat:=xlPart
    Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Replace What:="  ", Replacement:="", lookat:=xlPart
    End Sub
    Open enclosed file and run Macro1 in order to understand what Macro1 is doing...

    I need a macro which will do same operation of Macro1 but
    delete other values except "A" and "B" and "C"
    or
    replace other values except "A" and "B" and "C" to nothing.

    So, my aim is to shorten a Macro1.
    Best wishes.
    Attached Files Attached Files
    Last edited by HerryMarkowitz; 01-10-2014 at 07:49 AM.
    Sub DontForgetThese()
         If Your thread includes any code Then Please use code tags...
         If Your thread has been solved Then Please mark as solved...
         If Anybody has helped to you Then Please add reputation...
    End Sub

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Delete other values except "A" and "B" and "C"

    I reckon how you have it might be the quickest, the only way I could think of replacing everything except certain characters would be to loop through each character in the string and check it individually, or to use a formula to break the string into an array of characters and use a vba function to concatenate al the A's. B's and C's.

    That said, if you are just interested in the length of your code as a character count:
    Sub Macro1()
    With Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
       .replace ">","",xlPart
       .replace "B","",xlPart
       .replace "p","",xlPart
       .replace "t","",xlPart
       .replace " ","",xlPart
    End With
    End Sub
    is perhaps the same?

  3. #3
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: Delete other values except "A" and "B" and "C"

    Hi yudlugar,
    Thanks for the answer.
    I need this code because cell characters could be other characters not only ">", "B", "p", "t".
    So, I need a macro which will delete everything other than A, B and C.
    Any other advice will be thankful...

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Delete other values except "A" and "B" and "C"

    Function replace_but_ABC(str As String)
    Dim i
    For i = 1 To Len(str)
        If Mid(str, i, 1) = "A" Or Mid(str, i, 1) = "B" Or Mid(str, i, 1) = "C" Then
            replace_but_ABC = replace_but_ABC & Mid(str, i, 1)
        End If
    Next i
    End Function

  5. #5
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: Delete other values except "A" and "B" and "C"

    I prefer regular code instead of function. If possible ?

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Delete other values except "A" and "B" and "C"

    Sub replace_but_ABC()
    Dim i, str, replace_but_ABC
    str = "AAasdvjVBC"
    For i = 1 To Len(str)
        If Mid(str, i, 1) = "A" Or Mid(str, i, 1) = "B" Or Mid(str, i, 1) = "C" Then
            replace_but_ABC = replace_but_ABC & Mid(str, i, 1)
        End If
    Next i
    msgbox replace_but_ABC
    End Sub

  7. #7
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: Delete other values except "A" and "B" and "C"

    Not solved.
    But thanks anyway.

+ 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. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  2. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  3. Replies: 0
    Last Post: 01-09-2013, 06:52 PM
  4. Replies: 3
    Last Post: 02-16-2011, 02:55 PM
  5. Replies: 7
    Last Post: 05-13-2006, 05:02 PM

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