excel vba - Looping through an array in VBA -


in sheet1, cell a1 have following text: a-b-c-d-e-f

i need loop through text , have written following code works fine:

dim w worksheet dim s variant dim p integer  set w = worksheets(1)  p = 0 each s in split(w.range("a1").value, "-")     p = p + 1     msgbox split(w.range("a1").value, "-")(p - 1) next s 

the above code pops message box showing each of letters 1 after other, expected.

but not happy repeating of split(w.range("a1").value, "-"), declaring array loop , once again every occurrence within loop.

so have tried with:

msgbox s.value 

but throws error object being requested.

why can not use value property given "s" variant?

you can this

dim w worksheet dim s string  set w = worksheets(1)  each s in split(w.range("a1").value, "-")     msgbox s next 

when assign object variant behaves object assigned , has properties present in object. in each split assigned string s , string variables return value directly , not objstring.value.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -