-8

Please help me how to convert this cell on Excel from number to percent

the account: Banks - Global 0.308123555052582 --Internet Content & Information 0.307825851134657 --Consumer Electronics 0.383936697049749

to become

the account: Banks - Global 30.81% --Internet Content & Information 30.78% --Consumer Electronics 38.39%

Thank you.

BigBen
  • 46,229
  • 7
  • 24
  • 40
ngoc
  • 63
  • 6
  • I need to have formula to convert it. Thanks – ngoc Nov 15 '18 at 19:10
  • I have ton of cells like this that need formula to convert rather than manual change each number by hand – ngoc Nov 15 '18 at 19:12
  • I know some use substitute function but not sure how to use it – ngoc Nov 15 '18 at 19:15
  • Hello Big Ben , try this on excel on cell A1 and format as percentage – ngoc Nov 15 '18 at 19:17
  • not just number, use exact phrase . Thx – ngoc Nov 15 '18 at 19:20
  • Ahhhh. Your question made it sound like you had text in one cell and numbers in another, not text and numbers together. Maybe edit your question to explain that better. – BigBen Nov 15 '18 at 19:27
  • Yes, I' m sorry, Not sure how to use edit. I'm not tech savvy. – ngoc Nov 15 '18 at 19:31
  • they are all in one cell – ngoc Nov 15 '18 at 19:31
  • can't you insert a column and to split to columns so that you'll have the numbers in a separate cell? If you need to you can concat them back together afterwards – Chrisvdberge Nov 15 '18 at 20:02
  • I took a stab at editing the post - feel free to change it if that's not what you're looking for. – BigBen Nov 15 '18 at 20:13
  • Thanks for the work, but I don't think It would work cause I have more than 100 cells phrases like this and I don't think I can break it like the way you do. – ngoc Nov 15 '18 at 20:15

2 Answers2

0

Perhaps the following VBA solution using Regular Expressions.

Function FixMyPercentages(target As String) As String
    Dim output As String
    output = target
    With New RegExp
        .Global = False
        .MultiLine = True
        .IgnoreCase = False
        .pattern = "\s\d+\.\d+$|^\d+\.\d+\s|\s\d+\.\d+\s"

        Dim myMatch As Object, myMatches As Object

        Do While .test(output)
            Set myMatches = .Execute(output)
            For Each myMatch In myMatches
                output = .Replace(output, " " & Format$(CDbl(myMatch.Value), "0.00%") & " ")
            Next myMatch
        Loop
    End With
    FixMyPercentages = Trim(output)
End Function

enter image description here

To implement this:

  1. Alt + F11 to open the VB editor (or Developer > Visual Basic.)
  2. Then Insert > Module
  3. Paste the below code.
  4. Add a reference to Microsoft VBScript Regular Expressions 5.5 under Tools > References
  5. And then use it as a formula in your worksheet.

enter image description here

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Not sure how to use your work. Could you show me step by step. Many thanks. – ngoc Nov 16 '18 at 14:31
  • Got error at With New RegExp – ngoc Nov 16 '18 at 14:43
  • See the edit. You need to add the reference to `Microsoft VBScript Regular Expressions 5.5`. – BigBen Nov 16 '18 at 14:44
  • Oh wow, it works terrific. If the phrase with ' abc .123, def .456, zxt .789 ' then will this vb function work or need to change on VB function? Many thanks again. – ngoc Nov 16 '18 at 15:21
  • I got it. Thank you – ngoc Nov 16 '18 at 15:24
  • Sorry , I just test with the phrase " the account: --Household & Personal Products 0.356331914717454 " and the phrase " the account: --Internet Content & Information 0.379707380917121 --Consumer Electronics 0.61217039828375 ". Both are not work. Do we need to change on VB function? – ngoc Nov 16 '18 at 15:34
  • Getting rid of the space on the end should fix that. – BigBen Nov 16 '18 at 17:42
  • Again, many may thanks for your help. – ngoc Nov 16 '18 at 19:05
0

@BigBen has a nice sophisticated solution. I'd probably do a 'dirty workaround', something like:

  • In column A: find and replace a space followed by 0 by some divider ('|0')
  • insert helper column B
  • text to columns on column A, split on the divider put in place in step 1
  • format the cells in column B to be %
  • concatenate back together if needed.
Chrisvdberge
  • 1,824
  • 6
  • 24
  • 46
  • Thanks for the work, but I don't think It would work cause I have more than 100 cells phrases like this and I don't think I can break it like the way you do – ngoc Nov 15 '18 at 20:31