+ Reply to Thread
Results 1 to 7 of 7

Loop to create Object

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2015
    Location
    Jakarta
    MS-Off Ver
    2010
    Posts
    12

    Loop to create Object

    Hi again

    I tried to get the unique value of each column in the range "RD" and displays them in single column so I need to create an object ("scripting.Dictionary") are just as much the number of columns in Range "RD". I tried this code but result in "Run time error 91"
    Private Sub CommandButton1_Click()
    Range(Me.RefEdit1).Name = "RD"
    Range(Me.RefEdit2).Name = "OT"
    Dim d As Object, c As Variant, i As Long, s As Long
    Dim JK As Long
    Dim o As Collection
    JK = Range("RD").Columns.Count
    d = CreateObject("Scripting.Dictionary")
    For k = 0 To JK + 1
    c = Range("RD").Columns(k + 1)
    Set o = New Collection
    o.Name = "Name is " & k
    d.Add k, o
    For i = 1 To UBound(c, 1)
      o(c(i, 1)) = 1
    Next i
    Range("OT").Cells((k * 5) + 2, 2).Resize(d.Count) = Application.Transpose(d.Keys)
    Range("OT").Cells((k * 5) + 2, 2).Resize(d.Count).Sort Key1:=Range("OT").Cells((k * 5) + 2, 2).Resize(d.Count)
    Next k
    End Sub
    loop_to.xlsm
    any help will be appreciated

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this demonstration !

    PHP Code: 
    Sub Demo()
        [
    OT].CurrentRegion.Clear
        Application
    .ScreenUpdating False

        With CreateObject
    ("Scripting.Dictionary")
            For 
    Each V In [RD].Value:  .Item(V) = "":  Next
            
    [OT].Resize(.Count).Value Application.Transpose(.Keys)
            .
    RemoveAll
        End With
        
        
    [OT].CurrentRegion.Sort [OT]
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Registered User
    Join Date
    07-03-2015
    Location
    Jakarta
    MS-Off Ver
    2010
    Posts
    12

    Re: Hi ! Try this demonstration !

    Hi, marc L
    many thank for the reply
    your code worked fine but it just create a single "Scripting.Dictionary" so it produce unique values of whole columns instead of each one.
    here is file that shows expected result
    loop_to2.xlsm

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Thanks for the rep' !


    Was confused by single column

    PHP Code: 
    Sub Demo2()
        
    Application.ScreenUpdating False
        
    [RD].Rows(1)(0).Copy [OT]
        
    ReDim VA(1 To [RD].Rows.Count1 To [RD].Columns.Count)

        
    With CreateObject("System.Collections.SortedList")
            For 
    C& = 1 To [RD].Columns.Count
                
    For Each V In [RD].Columns(C).Value:  .Item(V) = "":  Next
                
    For R& = 1 To .Count:     VA(RC) = .GetKey(1):  Next
                
    .Clear
            Next
        End With

        
    [OT].Offset(1).Resize(UBound(VA), UBound(VA2)).Value VA
        
    [OT].CurrentRegion.HorizontalAlignment xlCenter
    End Sub 
    You may like it !

  5. #5
    Registered User
    Join Date
    07-03-2015
    Location
    Jakarta
    MS-Off Ver
    2010
    Posts
    12

    Re: Thanks for the rep' !

    Quote Originally Posted by Marc L View Post

    Was confused by single column

    PHP Code: 
    Sub Demo2()
        
    Application.ScreenUpdating False
        
    [RD].Rows(1)(0).Copy [OT]
        
    ReDim VA(1 To [RD].Rows.Count1 To [RD].Columns.Count)

        
    With CreateObject("System.Collections.SortedList")
            For 
    C& = 1 To [RD].Columns.Count
                
    For Each V In [RD].Columns(C).Value:  .Item(V) = "":  Next
                
    For R& = 1 To .Count:     VA(RC) = .GetKey(1):  Next
                
    .Clear
            Next
        End With

        
    [OT].Offset(1).Resize(UBound(VA), UBound(VA2)).Value VA
        
    [OT].CurrentRegion.HorizontalAlignment xlCenter
    End Sub 
    You may like it !
    wow, superb Marc L
    one last thing, can we display the result vertically in a single column that i'm talking about

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Single column …


    Clear U & V columns except titles …

    PHP Code: 
    Sub Demo1()
        
    ReDim VA(1 To [RD].Count1 To 2)
        [
    OT].CurrentRegion.Offset(1).Clear

        With CreateObject
    ("System.Collections.SortedList")
            For 
    C& = 1 To [RD].Columns.Count
                
    For Each V In [RD].Columns(C).Value:  .Item(V) = "":  Next
                                                 VA
    (R& + 11) = C
                
    For L& = 1 To .Count:   1:    VA(R2) = .GetKey(1):  Next
                
    .Clear
            Next
        End With

        
    [OT].Offset(1).Resize(R2).Value VA
    End Sub 
    You must like it ‼

  7. #7
    Registered User
    Join Date
    07-03-2015
    Location
    Jakarta
    MS-Off Ver
    2010
    Posts
    12

    Re: Single column …

    Quote Originally Posted by Marc L View Post

    Clear U & V columns except titles …

    PHP Code: 
    Sub Demo1()
        
    ReDim VA(1 To [RD].Count1 To 2)
        [
    OT].CurrentRegion.Offset(1).Clear

        With CreateObject
    ("System.Collections.SortedList")
            For 
    C& = 1 To [RD].Columns.Count
                
    For Each V In [RD].Columns(C).Value:  .Item(V) = "":  Next
                                                 VA
    (R& + 11) = C
                
    For L& = 1 To .Count:   1:    VA(R2) = .GetKey(1):  Next
                
    .Clear
            Next
        End With

        
    [OT].Offset(1).Resize(R2).Value VA
    End Sub 
    You must like it ‼
    great, youre genius Marc L
    thank you very much

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 06-19-2014, 02:09 PM
  2. In Procedure: When use Actual Range Object vs Create Local Object Using Set
    By Filibuster in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-20-2012, 02:21 PM
  3. While Loop Object Error
    By Ascension in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-04-2010, 03:04 PM
  4. Object Array / Loop
    By mpeplow in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-22-2007, 03:31 PM
  5. Using a variable as an object name in a loop
    By kevindmorgan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2007, 08:05 PM
  6. "RunTime error 429 ActiveX Object Cannot Create Object"
    By walid66 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-19-2006, 06:12 PM
  7. use a variable to name an object in a loop
    By m davidson in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-19-2006, 05:25 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1