Trim string from selected cells?

uprising

New Member
Joined
Nov 9, 2009
Messages
23
I have a macro that trims the first 7 characters of a string.
It works but only on one active cell at a time.
How do I make it work on a selected range of cells?
This is the code I have:

Code:
Sub Trimmail()
    Dim MyCell As Range
    On Error Resume Next
        For Each MyCell In Selection.Cells
            MyCell.Value = Right(Selection.Cells, Len(Selection.Cells) - 7)
        Next
    On Error GoTo 0
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Within your right() function (and within your len() function, but you can replace both with mid()), you should specify myCell instead of selection.cells. Try this:

Code:
Option Explicit
Sub Trimmail()
    Dim MyCell As Range
    On Error Resume Next
        For Each MyCell In Selection.Cells
            MyCell.Value = Mid(MyCell, 8)
        Next
    On Error GoTo 0
End Sub
 
Upvote 0
Try :-
Code:
Sub Trimmail()
    Dim MyCell As Range
    On Error Resume Next
        For Each MyCell In Selection.Cells
            MyCell.Value = Right(MyCell, Len(MyCell) - 7)
        Next
    On Error GoTo 0
End Sub
Mick
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top