VBA to trim down selected cell or cells

Stormrider2230

New Member
Joined
Dec 3, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I need help in trimming selected cells, from right, so that only first 14 characters (numbers) remain.

I have column filled with numbers but some of them have additional characters at the end so in total there are 14-18 or more numbers.

They are all numerical.

I need VBA that will keep the cell unchanged if there is only 14 numbers in it, or less, and if there are more, it will trim from the right, so only first 14 numbers will remain (or less if there are less).

I prefer it is done via selected cells.

I don't have any examples. I am very new in this. Don't quite understand variables to be able to apply similar solutions I found.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Is it any reason why You can't use:
Excel Formula:
=--(LEFT(A1,14))
 
Upvote 0
Solution
I am trying to avoid formulas. It's a huge sheet with data. I can't share it here.
I am looking for that or similar formula expressed in VBA. This is something I tried to put together. It's far from working example.
Plus, it needs a part to skip cells that are 14 or less characters.

VBA Code:
Sub RemoveRightTo14Num()
Dim cell As Range
Dim MyRange As Range
Dim tmp As String
Set MyRange = Selection  'this is the range of data
'remove char from right to keep 14 numbers
For Each cell In MyRange.Cells
   tmp = cell.Value
   If Len(tmp) > 14 Then
   cell.Value = Left(cell, 14)
   End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,947
Members
448,534
Latest member
benefuexx

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