vba help - split value and add to dictionary items

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I have a string value needs to split a string value, with comma Separated.

if Count of splitted value greater than 3 then

Add only 4 items to dictionary, First 3 value splitted and 4 the value all combined with space.

'Situation 1
str = "ABC 400 jan comment1 comment2"
'Situation 2
str2 = "ABC 500 Feb Comment1 Comment2 Comment3 Comment4"


Attempted code for situation1 , like that in situation 2 looking code.
Debug.Print dict(k)


VBA Code:
Sub test()

Dim str As String
Dim arr As Variant
Dim k As String
k = "Key"

Dim cnt As Long

'Situation 1
str = "ABC 400  jan comment1 comment2"

'Situation 2

str = "ABC 500 Feb  Comment1 Comment2 Comment3 Comment4"


'Below code for situation 1
Dim dict As New Scripting.Dictionary

arr = Split(str, " ")

cnt = UBound(arr)

If cnt > 3 Then
        arr(3) = arr(4) & " " & arr(5)
   
    dict.Add k, Array(arr(0), arr(1), arr(2), arr(3))
   
    Debug.Print dict(k)
       
   
End If


End Sub


Thanks
mg
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You can use the final argument in Split to limit it to 4 items
VBA Code:
arr = Split(str, " ", 4)

   
    dict.Add k, Array(arr(0), arr(1), arr(2), arr(3))
 
Upvote 0
Hi Fluff!

Wow amazing, thats Really nice ! (y)

one more help. how to print dict items one go Using debug.print.

dict.Add k, Array(arr(0), arr(1), arr(2), arr(3))
Debug.Print dict(k)



Thanks
mg
 
Upvote 0
Like
VBA Code:
    Debug.Print dict(k)(0), dict(k)(1), dict(k)(2), dict(k)(3)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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