PDA

View Full Version : [SOLVED:] How to use Array



idnoidno
07-21-2017, 06:51 AM
LString=” TechOnTheNet.com”

LArray = Split(LString, ".")

LArray() = Split(LString, ".")

LArray(i) = Split(LString, ".")

Are the above CODEs correct? Can someone explain or use examples?

SamT
07-21-2017, 07:13 AM
It is highly recommended that you have Option Explicit at the top of all code Modules.

Required:

Dim LArray As Variant
'OR
Dim LArray 'Is Variant when not specified as anything.


Dim Lstring As String
LString = ”TechOnTheNet.com, TechOnTheWeb.com, TechAndTheNet.com”

LArray = Split(LString, ".com, ")

'LArray(0) = TechOnTheNet
'LArray(1) = TechOnTheWeb
'LArray(2) = TechAndTheNet


See also: http://www.snb-vba.eu/VBA_Arrays_en.html

PS: Why is that the wrong value for LArray(2)?

Bob Phillips
07-21-2017, 04:08 PM
LArray(2) would hold TechAndTheNet.com with this code!


LArray = Split(Replace(Replace(Lstring, ".com", ""), " ", ""), ",")

will work.

mdmackillop
07-22-2017, 03:20 AM
Hi Bob
That gives me "TechAndTheNet"
For "TechAndTheNet.com" try LArray = Split(Replace(Lstring, " ", ""), ",")

Paul_Hossler
07-22-2017, 06:38 AM
Are the above CODEs correct? Can someone explain or use examples?


If you just want to separate the "TechOnTheNet" from the "com', something like this




Option Explicit
Sub SplitDemo()
Dim LArray As Variant
Dim LString As String

LString = "TechOnTheNet.com"


LArray = Split(LString, ".")

MsgBox LArray(0)
MsgBox LArray(1)
End Sub



That won't work if there are dots in the first part, so you can try this




Sub SplitDemo2()
Dim i As Long
Dim LString As String
Dim s1 As String, s2 As String

LString = "Tech.On.The.Net.com"

i = InStrRev(LString, ".")

s1 = Left(LString, i - 1)
s2 = Right(LString, Len(LString) - i)


MsgBox s1
MsgBox s2
End Sub

snb
07-22-2017, 07:02 AM
sub M_snb()
c00="TechOnTheNet.com"

msgbox typename(c00)
msgbox vartype(c00)
msgbox isarray(c00)

msgbox ubound(split(c00,"."))
msgbox lbound(split(c00,"."))

sn=split(c00,".")
msgbox typename(sn)
msgbox vartype(sn)
msgbox isarray(sn)
msgbox sn(0)
msgbox sn(1)
msgbox sn(lbound(sn))
msgbox sn(ubound(sn))

msgbox split(c00,".")(0)
msgbox split(c00,".")(1)
end sub

idnoidno
07-22-2017, 08:25 AM
I probably don't have enough basic knowledge, so asked a stupid question. LArray was declared variant ,
LArray = Split(LString, ".") ----->It is correct.


LArray() = Split(LString, ".") ----->Wrong.Except LArray() was declared.
LArray(i) = Split(LString, ".")------>Wrong. Same above.

SamT
07-22-2017, 08:31 AM
:yes

Paul_Hossler
07-23-2017, 07:54 AM
SamT forgot to mention that you can mark your thread solved by using [Thread Tools] above your first post

mdmackillop
07-23-2017, 08:08 AM
SamT forgot to mention that you can mark your thread solved by using [Thread Tools] above your first post
:rotflmao: