Converting a sub into function

SharmaAntriksh

New Member
Joined
Nov 8, 2017
Messages
31
Hello All,

I want to convert the below Sub Procedure into a function that I can call into Excel sheet.

Code:
Sub SplitTextIntoMultipleCells()
    
    Dim Counter As Long
    Dim WordArray() As Variant
    Dim WordList As Dictionary
    Dim dKey As Variant
    
    Set WordList = New Dictionary
    
    WordArray = Sheet1.Range("A1:A10")
    
    For Counter = LBound(WordArray, 1) To UBound(WordArray, 1)
        WordList.Add Counter, Split(WordArray(Counter, 1), " ")
    Next Counter
    
    For Each dKey In WordList.Keys
        Sheet1.Range(Range("D1").Offset(dKey - 1, 0), Range("D1").Offset(dKey - 1, UBound(WordList.Item(dKey)))).Value = WordList(dKey)
    Next dKey


End Sub

So far this is what I could do but I am not able to think of how to convert it fully into a function

Code:
Function SplitTextIntoMultipleCells(InputText As Range) As String
    
    Dim Counter As Long
    Dim WordArray() As Variant
    Dim WordList As Dictionary
    Dim dKey As Variant
    
    Set WordList = New Dictionary
    
    WordArray = InputText
    
    For Counter = LBound(WordArray, 1) To UBound(WordArray, 1)
        WordList.Add Key:=Counter, Value:=Split(WordArray(Counter, 1), " ")
    Next Counter
    
    For Each dKey In WordList.Keys
        Sheet1.Range(Range("D1").Offset(dKey - 1, 0), Range("D1").Offset(dKey - 1, UBound(WordList.Item(dKey)))).Value = WordList(dKey)
    Next dKey
    
End Function


Sub b()
    
    SplitTextIntoMultipleCells Range("A1:A10"), Range("E1")
    
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Why not just use a normal xl formula in D1 copied down & across
=TRIM(MID(SUBSTITUTE($A1," ",REPT(" ",500)),COLUMN(A1)*500-499,500))
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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