Consulting

Results 1 to 10 of 10

Thread: How to use Array

  1. #1

    How to use Array

    LString=” TechOnTheNet.com”

    LArray = Split(LString, ".")

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

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

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

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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)?
    Last edited by SamT; 07-22-2017 at 08:06 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    LArray(2) would hold TechAndTheNet.com with this code!

    LArray = Split(Replace(Replace(Lstring, ".com", ""), " ", ""), ",")
    will work.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Bob
    That gives me "TechAndTheNet"
    For "TechAndTheNet.com" try LArray = Split(Replace(Lstring, " ", ""), ",")
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by idnoidno View Post
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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
    Last edited by mdmackillop; 07-22-2017 at 07:24 AM. Reason: Missing brackets added.

  7. #7
    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.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    SamT forgot to mention that you can mark your thread solved by using [Thread Tools] above your first post
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Paul_Hossler View Post
    SamT forgot to mention that you can mark your thread solved by using [Thread Tools] above your first post
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •