+ Reply to Thread
Results 1 to 7 of 7

Filling an aray via vba

Hybrid View

  1. #1
    Registered User
    Join Date
    05-05-2015
    Location
    Rotterdam, The Netherlands
    MS-Off Ver
    Office 2007, 2010 and 2013
    Posts
    12

    Filling an aray via vba

    Hello,

    I am trying to learn some things about arrays. Can someone help me to make an understandable simple VBAcode. In column B I have some names, and in colum A I see the number of times these names have to be copied.
    In Range A2:F4 you 'll see the output as it should be. I found below mentioned VBA on internet, but I don't see how I can adapt this VBA to my situation.

    Sub From_sheet_make_array()
          Dim MyArray As Variant
          MyArray = Range("B2:B5").Value
    
          'Looping structure to look at array.
          For i = 1 To UBound(MyArray)
            MsgBox MyArray(i, 1)
          Next
    End Sub
    FillingArraysViaVBA.xlsb

    Thanks for your help/Excelbat

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Filling an aray via vba

    Is this a VBA exercise, or would a simple formula-based solution be OK?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    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()
            
    VA Blad1.Cells(1).CurrentRegion.Value
        
    For R& = 2 To UBound(VA)
            
    Blad1.Cells(R6).Resize(, VA(R1)).Value VA(R2)
        
    Next
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

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

    Lightbulb Another way for the fun !

    PHP Code: 
    Sub Test()
        
    With Blad1
                      VA 
    = .Cells(1).CurrentRegion.Value
            
    If UBound(VA) > 1 Then
                    M
    % = Application.Max(VA)
                For 
    R& = 2 To UBound(VA)
                    
    T$ = T$ & IIf(T$ > """;""") & _
                         Evaluate
    ("REPT(""""""" VA(R2) & """"",""," VA(R1) - ")") & _
                         
    """" VA(R2) & """" Evaluate("REPT("",""""""""""," VA(R1) & ")")
                
    Next
                    VA 
    Evaluate("{" "}")
                    .[
    F2].Resize(UBound(VA), UBound(VA2)).Value VA
            End 
    If
        
    End With
    End Sub 
    Last edited by Marc L; 10-22-2015 at 02:24 PM.

  5. #5
    Registered User
    Join Date
    05-05-2015
    Location
    Rotterdam, The Netherlands
    MS-Off Ver
    Office 2007, 2010 and 2013
    Posts
    12

    Re: Filling an aray via vba

    Hi everyone,

    @shg: Yes, this is a vba exercise.
    @Marc L #3: Thanks for your answer. This is what I looked for. This code is really understandable.
    @Marc L #4: De nouveau merci beaucoup pour votre aide. But this one is far beyond my level of VBA-knowledge. This one is really hard to understand.

    For others readers of this topic: I try to learn as much as I can from VBA and arrays, and every solution (simple, please) is welcome.

    Thanks/Excelbat

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

    Re: Filling an aray via vba


    Thanks for the rep' !

    To understand post #4, you need some Excel worksheet basics !

    An single array is written in an Excel formula like : {1,2,3} or {"A","B","C"}

    A two dimensions array is written like {1,2,3;"A","B","C"}

    Read the help of the powerful VBA Evaluate method !

    In VBA, an single array can be written like AR = [{1,2,3}] and a two dimensions like AR = [{1,2,3;"A","B","C}]

    Place a break point around the end of my post #4 code and
    in Locals window see T variable value and expand VA variable …

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

    Lightbulb Re: Filling an aray via vba


    To well understand array and Evaluate method, from that :

    PHP Code: 
    Sub Demo2a()
        
    With Blad1
            With 
    .Cells(1).CurrentRegion
                C
    & = Application.Max(.Columns(1).Value)
                
    R& = .Rows.Count 1
            End With
            With 
    .[F2].Resize(RC)
                 .
    Formula "=IF(COLUMN()-6<$A2,$B2,"""")"
                 
    .Formula = .Value
            End With
        End With
    End Sub 

    to this :

    PHP Code: 
    Sub Demo2b()
        
    With Blad1
            With 
    .Cells(1).CurrentRegion.Rows
                    R
    & = .Count 1
                With 
    .Item("2:" & .Count).Columns
                    C
    & = Application.Max(.Item(1).Value)
                    
    A$ = .Item(1).Address "," & .Item(2).Address
                End With
            End With
            With 
    .[F2].Resize(RC)
                 .
    Value = .Parent.Evaluate("IF(COLUMN(" & .Address ")-6<" ","""")")
            
    End With
        End With
    End Sub 

+ 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. Lookup partial fit in aray
    By Optim in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-13-2015, 09:53 PM
  2. Aray formula and Zeros
    By cdscivic in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-15-2013, 02:24 PM
  3. [SOLVED] Aray within an array
    By wacky_diva in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-22-2013, 12:49 AM
  4. Search arrays and delete duplicate aray
    By jacojvv in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2012, 07:44 AM
  5. query within vlookup table aray to retrive left 6
    By Shevyfan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-26-2011, 01:36 PM
  6. rearrange an aray
    By fly Catcher in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2008, 03:29 PM
  7. filling an aray without looping
    By mark@sentryi.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2005, 01:05 PM

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