Dividing the cell at exactly the right place

CaptainCsaba

Board Regular
Joined
Dec 8, 2017
Messages
78
Hi!

So here is the problem. We get a PDF report, and since it is not that well made, when we extract it into excel All the different columns get put into column one together. We only need a few lines from there that I managed to offset (so some cells in column D are empty) and from these lines we get to a point that is beyond my capabilities. We need to divide the cell into two different cells at exactly the right place, but because of the conversion this changes every time. I managed to figure out a way but I don't know how to put it into VBA. here are a few lines:

(Ref: ABN97) 1,353,255 JUPITER ASSET MANAGEMENT
(Ref: 16127) 325,000NFU MUTUAL

<tbody>
</tbody>

<tbody>
</tbody>

<tbody>
</tbody>
(Ref: 23860) 372,461JPMFAM

<tbody>
</tbody>
(Ref: 13465) 1,546,230 UBS ASSETMANAGEMENT

<tbody>
</tbody>
(Ref: ECE51) 370,915225 FRANKLINST

<tbody>
</tbody>
(Ref: EQA04) 56,593BLACKROCKINVESTMENT

<tbody>
</tbody>
(Ref: EQA15) 110,22117F 100,TOEGYE-RO
(Ref: 85789) 304,64211-3, HAMAMATSUCHO, 2-CHOME,

<tbody>
</tbody>

So here is how the rows work. First there is the ref code (red), then the shares amount (green),
then the manager or the address of the manager (orange).

<tbody>
</tbody>

From this we need the green in a cell and the orange part in a cell. (red is not needed). As you can see, the problem is that because of the bad conversion the end of the green part is not always the same. Sometimes it ends with a space, sometimes it is together and the worst is when there is an address number after it there is no space in between. I am open to all solutions., but what I thought of is this. The end of the ref code is always the same ") ". What the macro should do is it should find this point in the cell (by the way this is column D), and after that it should copy from one up to 3 numbers. If there is a comma "," after the 3rd number it should copy the 3 numbers after that and so on until there is no comma after the 3 number.

So in the end if the base text is in Cell D38 for example is
(Ref: EQA15) 110,22117F 100,TOEGYE-RO

<tbody>
</tbody>
then it should have 110,221 in Cell M38 and 17F 100,TOEGYE-RO in cell N38.

I hope this is possible. I am open to all solutions because this might not work 100% of the time, so some advice would be much appreciated.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
So sometimes there isnt a space between the green and the orange part?
 
Upvote 0
Also I forgot to mention, sometimes the number is below 1,000, so there is no comma in it. For example:

(Ref: ECG69) 931 6 ROUTE DE TREVES

<tbody>
</tbody>
 
Upvote 0
I cant even think of a logical way. Lets say your post number 4. If it said 9631ROUTE DE TREVES how could that logically split that number.
 
Upvote 0
I cant even think of a logical way. Lets say your post number 4. If it said 9631ROUTE DE TREVES how could that logically split that number.

In the PDF it is 3 columns (hence the 3 colors). 931 is the amount of shares managed by somebody who had the address of "6 Route de Treves" which is probably a bank located in Luxembourg. the problem is that Adobe acrobat has a hard time when he has to decide whether there should be a space between 931 and 6 when it puts 2 columns into one. Sometimes there is, sometimes there is none.

Gave me some headaches also. That is why I am asking you guys.
 
Last edited:
Upvote 0
OK let's try it like this. What would you guys do if we could safely assume that there is always a space after 3 digit numbers, like in the case of "931 6 Route de Treves"?
 
Upvote 0
What do you mean? There is always a space between the green and orange?
 
Upvote 0
What do you mean? There is always a space between the green and orange?

No. As I explained it above, the problem is that I can't solve it because sometimes there is a space and sometimes there is not. However I proposed a solution that I can't create in VBA. After that you mentioned, with a point, that that solution does not work because sometimes there could be 3 digit numbers and if there is no space after that and an address number then it could become a number where we can't determine where the 2 numbers should be divided. Therefore I said what if we created a formula where we use the solution I proposed for everything above 999 and for the cells below 1,000 we assume that there is a space (and hope that it will be like that). For everything 1,000 and above we should created something like this:
The end of the ref code is always the same ") ". What the macro should do is it should find this point in the cell (by the way this is column D), and after that it should copy from one up to 3 numbers. If there is a comma "," after the 3rd number it should copy the 3 numbers after that and so on until there is no comma after the 3 number.
 
Upvote 0
Ok try these:

Code:
Function AddSpace(c As Range)

x = c.Value
If InStr(x, ")") > 0 Then
    x = Trim(Mid(x, InStr(x, ")") + 1))
    For i = 1 To Len(x)
        If Mid(x, i, 1) = "," Then
            If Mid(x, i + 4, 1) <> "," And Mid(x, i + 4, 1) <> " " Then
                x = Left(x, i + 3) & " " & Mid(x, i + 4, Len(x))
            End If
        End If
    Next
End If
AddSpace = x
        
End Function

Function GetNumber(c As Range)

GetNumber = Left(AddSpace(c), InStr(AddSpace(c), " ")-1)
        
End Function

Function GetAddress(c As Range)

GetAddress = Mid(AddSpace(c), InStr(AddSpace(c), " ") + 1, Len(c))
        
End Function

Put them in a standard module then use them like =GetNumber(A1) or =GetAddress(A1)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,944
Members
449,198
Latest member
MhammadishaqKhan

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