VBA Format Cell based on number sets

jamesdean1379

Board Regular
Joined
Jun 11, 2014
Messages
55
Having some issues here.

I have a report that i pull multiple times a day and in Column L some cells have no data and others may have anywhere from 4 - 20 numbers. I have to go into the cells with the numbers and separate them by every fourth number and add UN in front of them.

Example

BEFORE
Cell L2
145689732148

AFTER
Cell L2
UN1456, UN8973, UN2148

Some cells in column L could be blank so i need it to keep the blank cells. Also, as i said, there could be 4 or 20 numbers in that one cell. I know lastrow could vary but could be set around 5,000.

Can anyone help with this?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Is VBA okay?
This would do what you want in the range L2 to L(where ever lastrow is):
Code:
Sub ConvertAllUN() 'Run this sub to convert all cells in L2:L???
Dim Cell As Range
Application.ScreenUpdating = False
For Each Cell In Range("L2:L" & Range("L" & Rows.Count).End(xlUp).Row)
    If Not Cell.Value = "" Then Cell.Value = ConvertToUN(Cell.Value)
Next Cell
End Sub

Function ConvertToUN(SrcValue As String) As String
Dim i As Long, UNValue As String, LenSrcValue As Long
LenSrcValue = Len(SrcValue)
For i = 4 To Len(SrcValue) Step 4
    UNValue = UNValue & "UN" & Mid(SrcValue, i - 3, 4) & ", "
Next i
ConvertToUN = Left(UNValue, Len(UNValue) - 2)
End Function
 
Last edited:
Upvote 0
When i run this, i keep getting a debug error at the line "Run-Time error '1004' Range of object global failed

Code:
For Each Cell In Range("L2:L2000" & Range("L" & Rows.Count).End(xlUp).Row)
 
Upvote 0
Get rid of the "2000", the "Range("L" & Rows.Count).End(xlUp).Row" part will automatically find the last used row in column L
Code:
For Each Cell In Range("L2:L[COLOR=#ff0000]2000[/COLOR]" & [COLOR=#0000ff]Range("L" & Rows.Count).End(xlUp).Row[/COLOR])
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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