Results 1 to 10 of 10

Thread: Deleting part of a text in a cell in Excel using VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Resolved Deleting part of a text in a cell in Excel using VB

    Hi,

    Just a quick question - is it possible to delete a part of a cells content.

    ex.

    cell A1 says "345 Text" and I would like to delete 345, so it just states "text"

    In my worksheet I have some 50 companies, but in front of the company name there is a 5-digit registration number - it is this number I would like to delete

    Is this possible??
    Last edited by direktoren; Feb 22nd, 2006 at 03:30 AM.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Deleting part of a text in a cell in Excel using VB

    Say u have a range A1 to A50 that has the names...


    VB Code:
    1. Dim tmp As String
    2. For x = 1 To 50
    3.      tmp = xls.Range("A" & x)
    4.      xls.Range("A" & x) = Trim(Right(tmp,Len(tmp)-5))
    5. Next
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Deleting part of a text in a cell in Excel using VB

    Quote Originally Posted by Static
    Say u have a range A1 to A50 that has the names...
    VB Code:
    1. Dim tmp As String
    2. For x = 1 To 50
    3.      tmp = xls.Range("A" & x)
    4.      xls.Range("A" & x) = Trim(Right(tmp,Len(tmp)-5))
    5. Next
    Great code!

    I would like to make one change on that. If the cell is empty then it will throw the error. So, we need to avoid that error:
    VB Code:
    1. Dim x As Integer
    2. Dim tmp As String
    3. For x = 1 To 5
    4.      tmp = Range("A" & x)
    5.      If Not Len(tmp) = 0 Then
    6.      Range("A" & x) = Trim(Right(tmp, Len(tmp) - 5))
    7.      End If
    8. Next
    CS

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Deleting part of a text in a cell in Excel using VB

    Bah.. technicalities!! LOL

    good thinkin CS....
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Lively Member
    Join Date
    Nov 2005
    Location
    Oxford UK
    Posts
    76

    Re: Deleting part of a text in a cell in Excel using VB

    How would you make this work, it keeps looping??
    Code:
    Sub Remove()
    Dim numnRep As Long
     numRep = "0"
    
    Do While numRep <> ""
      Sheet1.Range("a1", Range("a100").End(xlUp)).replace What:=numRep, Replacement:="", LookAt:=xlValue, _
        SearchOrder:=xlByRows, MatchCase:=False
      numRep = numRep + 1
    Loop
    
    End Sub

  6. #6
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Deleting part of a text in a cell in Excel using VB

    Quote Originally Posted by thelocaluk
    How would you make this work, it keeps looping??
    Code:
    Sub Remove()
    Dim numnRep As Long
     numRep = "0"
    
    Do While numRep <> ""
      Sheet1.Range("a1", Range("a100").End(xlUp)).replace What:=numRep, Replacement:="", LookAt:=xlValue, _
        SearchOrder:=xlByRows, MatchCase:=False
      numRep = numRep + 1
    Loop
    
    End Sub
    Your code has lots of confusion:
    1. in the beginning you are declaring numnRep As Long
    2. Next you are assigning 0 to numRep (which is not numnRep)
    3. in Loop you are incrementing 1 everytime to numRep.

    There are some data type issues. You need to check and correct it.

    If it is a new thread please post it separately.
    CS

  7. #7
    Lively Member
    Join Date
    Nov 2005
    Location
    Oxford UK
    Posts
    76

    Re: Deleting part of a text in a cell in Excel using VB

    It's related to the same thread, to remove the numbers from cells that also contain letters. This code removes the 0's then 1's,2's,ect. Yes i should declare
    Code:
    Dim numnRep As Long
    as
    Code:
    Dim numRep As String
    , but how do I stop the loop??

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Deleting part of a text in a cell in Excel using VB

    no u are making it too complicated...

    u dont want to use the replace....
    if the 1st 5 characters in the cell are the reg number and u want to remove them:


    VB Code:
    1. Public Sub Remove(Col As String)
    2.     Dim Row As Integer
    3.     Dim t() As String
    4.     t = Split(ActiveSheet.UsedRange.Address, "$")
    5.     Row = t(UBound(t))
    6.      Dim x As Integer
    7.      Dim tmp As String
    8.      For x = 1 To Row
    9.           tmp = Range(Col & x)
    10.           If Not Len(tmp) = 0 Then
    11.                Range(Col & x) = Trim(Right(tmp, Len(tmp) - 5))
    12.           End If
    13.      Next
    14. End Sub

    usage:

    Remove "A"

    will fix column A
    it will find the used range of the sheet.. grab the end row.. then loop through and clip off the first 5 characters...
    I would stay away from find replace.. it causes errors in the code if it doesnt find anything
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    Lively Member
    Join Date
    Nov 2005
    Location
    Oxford UK
    Posts
    76

    Thumbs up Re: Deleting part of a text in a cell in Excel using VB

    Thank's, I'm learning every day

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Deleting part of a text in a cell in Excel using VB

    As the actual poser of the question, let me give my utmost appreciation - I had never thought of that one myself - didn't even know the trim function...

    thanx
    /nick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width