looping

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
I'm trying to loop through what I call sentences. Below the find_sentences2 function will identify the sentences that compose an active cell. So

(xHy) & (xHz) & (bHc)

The find_sentences2 will store the above 3 sentences between parentheses into an array. I can't figure out how to loop through it. Till now, I've just been putting a number between the parentheses after atomicsent which will identify which sentence I'm analyzing.



Code:
Dim input3 As Variant
dim input1 as string

input1 = activecell.Value


atomicsent = find_sentences2(input1)


input3 = atomicsent()


For Each input3 In atomicsent


(code)

Next
 

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.
i guess you are looking for something like this

Code:
Function LoopThroughME(s As String)
Dim mArray As Variant
Dim ArrayCounter As Integer

mArray = Split(Replace(Replace(s, "(", ""), ")", ""), "&")

For ArrayCounter = LBound(mArray) To UBound(mArray)
    MsgBox mArray(ArrayCounter)
Next

End Function
 
Upvote 0
i guess you are looking for something like this

Code:
Function LoopThroughME(s As String)
Dim mArray As Variant
Dim ArrayCounter As Integer

mArray = Split(Replace(Replace(s, "(", ""), ")", ""), "&")

For ArrayCounter = LBound(mArray) To UBound(mArray)
    MsgBox mArray(ArrayCounter)
Next

End Function
While you declared it as a Function, your code does not return anything, so it should more properly be declared as a Sub, I would think.

Here is a function that returns a one-dimensional array (starting element number is 1, not 0)...
Code:
Function find_sentences2(S As String) As String()
  Dim X As Long, Parts() As String, Temp() As String
  Parts = Split(Replace(S, ")", "("), "(")
  ReDim Temp(1 To Int(UBound(Parts)) / 2)
  For X = 1 To UBound(Parts) Step 2
    Temp((X + 1) / 2) = Parts(X)
  Next
  find_sentences2 = Temp
End Function
 
Upvote 0
I know I was just replying to him about how to go through the sentences and he wanted a function
 
Upvote 0
actually the algorithm for identifying a sentence is much more complex than just looking at the parentheses and an &, but don't worry the function find_sentences already does that. I'm able to loop through it now but I'm having trouble because it is a dynamic array. When I do the code this way, it works:

Code:
atomicsent = find_sentences2(input1)


For i = 1 To 3


input3 = atomicsent(i)

(code)

next

But when I try to do it this way, the value for the input3 will be empty on the next loop:

Code:
atomicsent = find_sentences2(input1)


For i = lbound(atomicsent) To ubound(atomicsent)


input3 = atomicsent(i)

(code)

next

Maybe I need to use redim but i don't know how.
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,101
Members
449,066
Latest member
Andyg666

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