How to split units from numbers stored in a cell that is custom formatted

jhilamroy

New Member
Joined
Sep 14, 2019
Messages
3
Hi All! I am new to this forum. Need help.

I need to separate units from quantities. i am not being able to do it because cells that are custom formatted and no text function works on these. When converted to text format, the units automatically disappears! Can you please help me out on this.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If the units are only part of the custom format, you'd need code to retrieve them - do you need them, or the quantities, or both?
 
Upvote 0
In this case try the code
Code:
Sub test()
    a = Range("a1").Text
    a = Split(a, Left(a, 1))
    Cells(1, 2) = a(1)
End Sub

Copy the code
Alt+f11
Insert>module> paste
Go Back to excel sheet>view>macros>view macros>select test and hit Run
 
Last edited:
Upvote 0
Thanks Rory for quick response! Yes the units are part of the custom format. Any non-code solution would have been better no? If not possible, then please share the code and guide how to apply it on my worksheet please!
 
Upvote 0
Thanks for the code! but not solving my problem.

I have something like the following as my input:

ITEM QUANTITY
A 0.27 Tons
B 0.35 MT
C 300.5748 METER
B 65.248 Litre

And the output expected will be like this:

ITEM QUANTITY UoM
A 0.27 Tons
B 0.35 MT
C 300.5748 METER
B 65.248 Litre

Now since the QUANTITY field is custom formatted, no function works on them. That is my issue.
 
Upvote 0
Well in you case
data in column A
Code:
Sub test()
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 1 To lr
          a = Split(Range("a" & i).Text, " ")
          Cells(i, 2) = a(0) & " " & a(1)
    Next
End Sub
 
Last edited:
Upvote 0
Or may be
Code:
Sub test()  
  lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 1 To lr
        a = Split(Range("a" & i).Text, " ")
        Cells(i, 2) = a(0) & " " & a(1)
        Cells(i, 3) = a(2)
    Next
End Sub
 
Last edited:
Upvote 0
I have something like the following as my input:

ITEM QUANTITY
A 0.27 Tons
B 0.35 MT
C 300.5748 METER
B 65.248 Litre
So are you saying Column A has letters like A, B, C in it and Column B has your formatted numbers with each Column's values starting on Row 2 (headers being on Row 1)? If so, give this macro a try...
Code:
Sub SplitFormattedValues()
  Dim Cell As Range, ValQuant() As String
  For Each Cell In Range("B2", Cells(Rows.Count, "B").End(xlUp))
    Cell.Offset(, 1) = Split(Cell.Text & " ")(1)
    Cell.ClearFormats
  Next
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (SplitFormattedValues) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:
Upvote 0
What about this macro
Code:
Option Explicit
Sub test()
Dim obj As Object, lr%, i%, Ys
Set obj = CreateObject("VBScript.RegExp")
lr = Cells(Rows.Count, 1).End(xlUp).Row
obj.Pattern = "(\.\d*)"
  For i = 1 To lr
    If obj.test(Range("a" & i)) Then
     Set Ys = obj.Execute(Range("a" & i))
      Range("b" & i) = Ys(0)
    End If
  Next i
End Sub
 
Upvote 0
What About This Macro
Code:
Option Explicit
Sub test()
Dim obj As Object, lr%, i%, I_find


Set obj = CreateObject("VBScript.RegExp")
lr = Cells(Rows.Count, 1).End(xlUp).Row
obj.Pattern = "(\.?\d+)"
  For i = 1 To lr
    If obj.test(Range("a" & i)) Then
     Set I_find = obj.Execute(Range("a" & i))
          Range("b" & i) = I_find(0).Value
    End If
  Next i
End Sub
I know that I should delete my first post
But I don't know how
Please Explain
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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