vb shortening the value of a cell

asylum

Board Regular
Joined
Dec 2, 2003
Messages
243
Hi,

Can you tell me what is wrong with the below? I am trying to remove the far right character from the values in any cell in the range, but it keeps saying i have an invalid proceedure call or argument at:R.Value = Left(R.Value, l)

Thanks



Sub shorten()

Dim R As Range
Dim l As Integer

For Each R In Range("A1:a300")

l = Len(R.Value) - 1

R.Value = Left(R.Value, l)

Next R

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You have defined your variable l as an Integer, but Left() requires a Long value.

Change

Dim l As Integer

to

Dim l As Long
 
Upvote 0
If R is an empty cell, l wiil be -1 and that could cause an error.

Maybe try something like this...
Code:
Sub shorten()

Dim R As Range

For Each R In Range("A1:a300")

If Len(R.Value) > 1 Then R.Value = Left(R.Value, Len(R.Value) - 1)

Next R

End Sub
 
Upvote 0
I think AlphaFrog has it though.

Try

Code:
If l > 0 Then
R.Value = Left(R.Value, l)
End If
 
Upvote 0
Try:-
Code:
If l >= 0 Then R.Value = Left(R.Value, l)
 
Upvote 0
Wow,

Thanks very much guys for all the quick responses, yes you all had goo dvariations on teh theme that I had forgotten to allow for exceptions (such as the cell being blank!).

once again simple when shown how! - cheers

Andy
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

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