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
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

taigovinda

Well-known Member
Joined
Mar 28, 2007
Messages
2,639
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
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,474
Messages
5,601,880
Members
414,479
Latest member
Beau the dog

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
Top