+ Reply to Thread
Results 1 to 7 of 7

Macro to truncate string... like "Len,Left,Right formulas"

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Macro to truncate string... like "Len,Left,Right formulas"

    I would like a macro to identify the cell to the right, shorten the string of text and place it in the active cell. I want to snip characters off both the left and right sides.

    I would like a macro to perform as follows:

    Run macro

    Input box: "Enter the number of characters to delete from the left"

    **I'll enter... say "7"

    Input box: "Enter the number of characters to delete from the right"

    **I'll enter... say "4"

    **Then in the active cell it will produce a truncated string.

    Any help appreciated!!
    Last edited by Xx7; 02-28-2011 at 11:17 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro to truncate string... like "Len,Left,Right formulas"

    Hello Xx7,

    Will the macro always perform both the left and right truncations together or separately?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: Macro to truncate string... like "Len,Left,Right formulas"

    Quote Originally Posted by Leith Ross View Post
    Hello Xx7,

    Will the macro always perform both the left and right truncations together or separately?
    Hi Leith,

    I believe together would be best

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro to truncate string... like "Len,Left,Right formulas"

    Hello Xx7,

    Perhaps this macro will work for what you want to do.
    Sub Truncate()
    
      Dim Lcount As Integer
      Dim NewText As String
      Dim Rcount As Integer
      Dim Response As Variant
      Dim Text As String
      
        Response = InputBox("Enter the number of characters to remove from the left.")
          Lcount = Val(Response)
          
          If Lcount = 0 Then Exit Sub
        
        Response = InputBox("Enter the number of characters to remove from the right.")
          Rcount = Val(Response)
          
          If Rcount = 0 Then Exit Sub
          
        Text = ActiveCell.Text
        
          If Text <> "" Then
             NewText = Mid(Text, Lcount + 1, Len(Text) - Lcount)
             NewText = Mid(NewText, 1, Len(NewText) - Rcount)
          End If
          
        ActiveCell.Offset(0, 1).Value = NewText
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: Macro to truncate string... like "Len,Left,Right formulas"

    Is there anyway to do this for a range of cells, so I don't have to run it for each individual cell. For example, if I have a column filled with strings, I would like to select a bunch of cells in the column, run the macro, and then all the cells to the right of the selected cells would be truncated.

    Sorry, i'm a newb.
    thx

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro to truncate string... like "Len,Left,Right formulas"

    Hello Xx7,

    This will ask you to select the range of cells to truncate.
    Sub TruncateRange()
    
      Dim Cell As Range
      Dim Lcount As Integer
      Dim Rng As Range
      Dim NewText As String
      Dim Rcount As Integer
      Dim Response As Variant
      Dim Text As String
      
        Response = InputBox("Enter the number of characters to remove from the left.")
          Lcount = Val(Response)
          
          If Lcount = 0 Then Exit Sub
        
        Response = InputBox("Enter the number of characters to remove from the right.")
          Rcount = Val(Response)
          
          If Rcount = 0 Then Exit Sub
          
        On Error Resume Next
          Set Rng = Application.InputBox("Select the cells you want to truncate.", Type:=8)
          If Err <> 0 Then Exit Sub
          
          For Each Cell In Rng
            Text = Cell.Text
        
            If Text <> "" Then
               NewText = Mid(Text, Lcount + 1, Len(Text) - Lcount)
               NewText = Mid(NewText, 1, Len(NewText) - Rcount)
            End If
          
           Cell.Offset(0, 1).Value = NewText
        
          Next Cell
          
    End Sub

+ 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