Consulting

Results 1 to 7 of 7

Thread: Array

  1. #1

    Unhappy Array

    i have code like this
    Option Base 1
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim t, u As Long
    t = Array("John", "Mark", "Darko","Frank","Georg","Petar","Bob","Natali","Tina")
    For u = LBound(t) To UBound(t)
        Range("A:A").Replace u, t(u)
        
    Next
    End Sub
    code like this work perfect but
    if i add more name in array i get wrong resualt...
    i need 15 name
    any help is good
    txn

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    What wrong result do you get?

    Are you trying to replace numbers (1 thru 15) with Names?
    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
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Of course 11 wil be replaced by "JohnJohn"

    But you also should avoid doing this in the eventprocedure 'SelectionChange'.

  4. #4
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    You could do ubound to lbound to counter the "johnjohn" problem but I would probably avoid replace in this instance and test the cell value, too much potential for error.

    Do you have a few rows of sample data?

  5. #5
    i fix this problem like this
    Option Base 1
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim t, u As Long
    t = Array("John", "Mark", "Darko","Frank","Georg","Petar","Bob","Natali","Tina")
    For u = LBound(t) To UBound(t)
        Range("A:A").Replace u, t(u),xlwhole
        
    Next End Sub

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    I'd use

    Sub M_snb()
      sn=Array("","John", "Mark", "Darko","Frank","Georg","Petar","Bob","Natali","Tina")
      For j=1 to ubound(sn)
        columns(1).Replace j, sn(j), 1
      Next
    End Sub

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Just a guess, but I'd say that JohnJohn was the problem.
    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

Posting Permissions

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