Excel SPLIT only first 3 matches

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello community,

I am using following VBA to split string by characters. But when I use UBound , I get problems, because in some columns it splits more than I need.
Is there possibility to SPLIT only by first 3 characters?

VBA Code:
MyArray = Split(openPos, "<td>")
For N = 0 To UBound(MyArray)
    Cells(2 + i, N + 65).Value = MyArray(N)
Next N
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
MyArray = Split(openPos, "<td>",3) <-- this way it splits to 3 places. But third split returns whole text.. If any one can help me to do it to split 1,2,3 untill 4..?
 
Upvote 0
Depend on your data, if UBound(MyArray) is always at least 2 then try:
VBA Code:
MyArray = Split(openPos, "<td>")
For n = 0 To 2
    Cells(2 + i, n + 65).Value = MyArray(n)
Next n

if UBound(MyArray) could be any number, try:
VBA Code:
MyArray = Split(openPos, "<td>")
For n = 0 To UBound(MyArray)
    Cells(2 + i, n + 65).Value = MyArray(n)
    If n = 2 Then Exit For
Next n
 
Upvote 0
My code in post #3 doesn't really change the array, it's just limiting the entries that are sent to the range.
If you want to change the array itself, try:
VBA Code:
MyArray = Split(openPos, "<td>")
If UBound(MyArray) > 2 Then ReDim Preserve MyArray(0 To 2)

For n = 0 To UBound(MyArray)
    Cells(2 + i, n + 65).Value = MyArray(n)
Next n
 
Upvote 0
Thanks for reply, will try and let you know if it worked.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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