MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Get all possible combinations of words in sentence

  Asked By: Fareedah    Date: Aug 12    Category: MS Office    Views: 1364

Please correct this code.

Public Sub aa()
Dim myWordsArray

Dim mySentence As String

mySentence = "Hello have a nice day"
myWordsArray = Split(mySentence, " ")
For i = 0 To UBound(myWordsArray)
'MsgBox myWordsArray(i)
Next i


For i = 0 To UBound(myWordsArray)
For y = i + 1 To UBound(myWordsArray)
varT = varT & myWordsArray(y) & " " & myWordsArray(y + 1) & vbNewLine
Next y
varT = varT & myWordsArray(0) & vbNewLine
Next i
ActiveCell.Value = varT
End Sub



4 Answers Found

Answer #1    Answered By: Hubba Akhtar     Answered On: Aug 12

what is it doing wrong???????????????????????????????

Answer #2    Answered By: Sumitra 2004     Answered On: Aug 12

Its not giving the desired output.
I need all possible combinations  of words  within a sentence.

Answer #3    Answered By: Betty White     Answered On: Aug 12

Adapting from here:
and taking a few lazy shortcuts along the way I got this:

Dim myWordsArray
Dim CurrentRow

Sub GetString()
Dim mySentence As String
mySentence = "Hello have a nice day"
myWordsArray = Split(mySentence, " ")
Dim InString As String
InString = Left("0123456789", UBound(myWordsArray) + 1)
If Len(InString) < 2 Then Exit Sub
If Len(S) >= 8 Then
MsgBox "Too many permutations!"
Exit Sub
CurrentRow = 1
Call GetPermutation("", InString)
End If
End Sub

Sub GetPermutation(x As String, y As String)
' The source of this algorithm is unknown
Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
StrVarT = x & y
VarT = ""
For k = 1 To Len(StrVarT)
VarT = VarT & myWordsArray(Val(Mid(StrVarT, k, 1))) & " "
Next k
Cells(CurrentRow, 1) = Application.WorksheetFunction.Trim(VarT)
CurrentRow = CurrentRow + 1
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), _
Left(y, i - 1) + Right(y, j - i))
End If
End Sub

Paste the above into a module, making sure the top two lines (Dim
statements) are at the top of the module in the declarations area, and
run GetString. It clears anything in the column A of the active sheet
so be careful.

Answer #4    Answered By: Beverly Brooks     Answered On: Aug 12

Maybe I wasn't clear with the requirement.

Sentence : "I use microsoft office"

Possible combinations
I use microsoft
I use
use microsoft office
use microsoft
microsoft office
I use office
I microsoft office

This seems to be very complex and I have dropped the problem.

Didn't find what you were looking for? Find more on Get all possible combinations of words in sentence Or get search suggestion and latest updates.