-1

I have my column B with Strings like "6L2AAB". My column D with Strings like "E3" I'd like to produce in my column J the concatenation of B&D, for instance "6L2AABE3", for each row

My code throws a "13" error

.Range("J:J").Value = .Range("B:B").Value & "" & .Range("D:D").Value

Is there a way to do this without a loop ? Thanks

2 Answers 2

6

Edit: added loop-based approach to compare timing. Loop is faster!

Your code doesn't work because (eg) .Range("B:B").Value returns a 2-dimensional array, and you can't concatenate the contents of two arrays using &

You can use the worksheet's Evaluate method:

Sub tester()
    
    Dim t, i As Long, arr1, arr2, arr3, x As Long
    
    t = Timer
    With ActiveSheet
        .Range("J:J").Value = .Evaluate("=B:B&D:D")
    End With
    Debug.Print "Evaluate", Timer - t
    
    
    t = Timer
    With ActiveSheet
        arr1 = .Range("B:B").Value 'read input values
        arr2 = .Range("D:D").Value
        ReDim arr3(1 To UBound(arr1), 1 To 1) 'size array for output
        
        For i = 1 To UBound(arr1, 1)          'loop and concatenate
            arr3(i, 1) = arr1(i, 1) & arr2(i, 1)
        Next i
        .Range("J:J").Value = arr3 'populate output to sheet
    End With
    Debug.Print "Loop", Timer - t
    
End Sub

Maybe don't run it on the whole column unless you really need that though.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm curious if this is faster than looping through an array. Any experiences?
I've never timed a comparison. I only offered this because OP mentioned "no loops" but I'd probably use a loop myself, to allow for any logic/rules which might creep in later around how the concatenation should occur (ie. handling missing values etc)
Loop is about 10-15% faster than Evaluate
0

@Tim Williams It really depends on the number of rows, if you are working with larger amounts of data loops start to slow slightly and evaluate will jump ahead in processing speed just slightly. But there is another option that is faster than both, the .formula method. See Tim's code with it added in below using > 1 million rows with .formula added.

Average for me:

Formula 1.664063

Evaluate 3.675781

Loop 3.824219

Sub tester()
    
    Dim t, i As Long, arr1, arr2, arr3, x As Long
    
    t = Timer
    With ActiveSheet
        .Range("C:C").Formula = "=A:A&B:B"
    End With
    Debug.Print "Formula", Timer - t
    
    t = Timer
    With ActiveSheet
        .Range("C:C").Value = .Evaluate("=A:A&B:B")
    End With
    Debug.Print "Evaluate", Timer - t
    
    
    t = Timer
    With ActiveSheet
        arr1 = .Range("A:A").Value 'read input values
        arr2 = .Range("B:B").Value
        ReDim arr3(1 To UBound(arr1), 1 To 1) 'size array for output
        
        For i = 1 To UBound(arr1, 1)          'loop and concatenate
            arr3(i, 1) = arr1(i, 1) & arr2(i, 1)
        Next i
        .Range("C:C").Value = arr3 'populate output to sheet
    End With
    Debug.Print "Loop", Timer - t
    
End Sub

4 Comments

.Formula isn't faster if you want the methods to give the same end-point of cells populated with values (since you need the extra step of converting the cells with formulas to values, which makes it the slowest of the 3...)
@TimWilliams With no doubt a Loop will be the fastest way. - Using, however a single-cell spill range target in an empty column (to prevent an overflow error) in vers. 2019+/MS365 including the value reconversion of the entire columns would speed up the Formula|Formula2 approach at least to a time comparable to the Evaluate approach.
If you need to convert it the formula to values (if you really need to) then yes it will slow it down. But this all depends on the amount of data. VBA like VBScript exponentially increases the time to run when larger amounts of data is used, whereas excel formulas/evaluate are C based. Since excel has a 1.4 mill+ row limit is doesn't affect it quite as much. But somewhere around 500k rows Evaluate overtakes looping as the fastest method.
I can't replicate that - loop is consistently faster than evaluate in my testing up to 900k rows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.