+ Reply to Thread
Results 1 to 4 of 4

2D VBA array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    2D VBA array

    I know my problem is better done as a simple formula but I'm trying to get my head around arrays in VBA so this is an educational process rather than an efficiency one.

    I have "First name" in Col A and "Surname" in Col b and want to have Col C as "First Name Surname"
    With just one name I do this and it works fine.
    Sub philarray()
    Dim myarray() As Variant
    myarray = Range("a1:b1").Value
    Range("c1") = myarray(1, 1) & " " & myarray(1, 2)
    But with multiple names I do this and it doesn't work

    Sub morearrays()
    Dim lrow As Integer
    Dim secondarray() As Variant
    Dim x As Integer
    Range("a1").Activate
    lrow = Cells(Rows.Count, 1).End(xlUp).Row
    secondarray = Range("a2:b" & lrow).Value
    x = 1
    For Each Item In secondarray
    x = x + 1
    Range("c2:c" & lrow).Value = secondarray(x, 1) & " " & secondarray(x, 2)
    Next
    
    
    End Sub
    It's almost there as it copies each name and pastes in C but rather than each name in a single list it will copy a single name down the column and loop through each name like that. I have tried For Next which didn't work so any educational help would be appreciated.

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: 2D VBA array

    Hi,

    You don't want to loop through each item in the array, only each row, so you can do something like this
    Sub morearrays()
    Dim lrow As Long
    Dim secondarray() As Variant
    Dim x As Long
    lrow = Cells(Rows.Count, 1).End(xlUp).Row
    secondarray = Range("a2:b" & lrow).Value
    
    For x = 1 to Ubound(secondarray, 1)
    Cells(x + 1, "c").Value = secondarray(x, 1) & " " & secondarray(x, 2)
    Next
    
    
    End Sub
    It would be more efficient to write the results into another array and load that into column C at the end
    Sub morearrays()
    Dim lrow As Long
    Dim secondarray() As Variant
    Dim thirdArray()
    Dim x As Long
    lrow = Cells(Rows.Count, 1).End(xlUp).Row
    secondarray = Range("a2:b" & lrow).Value
    
    ' resize new array to same number of rows and one column
    Redim thirdArray(1 to ubound(secondarray, 1), 1 to 1)
    
    For x = 1 to Ubound(secondarray, 1)
    ' load into array instead of cells
    thirdArray(x, 1) = secondarray(x, 1) & " " & secondarray(x, 2)
    Next
    
    ' now populate column C
    Range("C2:C" & lrow).Value = thirdArray
    
    End Sub
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Contributor
    Join Date
    08-04-2004
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    186

    Re: 2D VBA array

    Thanks!
    So Ii was right with For Next but made noobie error by using Range rather than cells and learnt a new function with UBound.
    My code actually works if i use your cells line but I get an error at the end which I guess is a result of not using Ubound

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,994

    Re: 2D VBA array

    Late to the party but here's a variation on a theme using just the one array:

    Option Explicit
    
    Sub CombineNames()
    
    Dim ws As Worksheet
    Dim lLR As Long, i As Long
    Dim vArr
    
    Set ws = Sheet1
    
    With ws
        lLR = .Range("A" & .Rows.Count).End(xlUp).Row
        vArr = .Range("A2:B" & lLR)
        ReDim Preserve vArr(LBound(vArr, 1) To UBound(vArr, 1), LBound(vArr, 2) To UBound(vArr, 2) + 1)
        
        For i = LBound(vArr, 1) To UBound(vArr, 1)
            vArr(i, 3) = vArr(i, 1) & " " & vArr(i, 2)
        Next 'i
        
        .Range("A2:C" & lLR) = vArr
    End With
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ 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. [SOLVED] Array formulae with different sized array, e.g. array-if() or somproduct-if()?
    By hlhans in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-09-2016, 03:59 PM
  2. Replies: 3
    Last Post: 04-02-2016, 08:16 PM
  3. [SOLVED] Populate one array from another array and print new array as a range
    By Kaden265 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2014, 07:52 AM
  4. [SOLVED] Quick Array question - Copy array to another array then resize?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-02-2013, 01:17 AM
  5. [SOLVED] Populate Listbox with all rows of a dynamic array where elements of a single array match.
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-07-2012, 04:54 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