Evaluate instead of loop for 2 column concatenation

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Have this code:
Code:
Sub Add_Column_for_Pivot()

    Dim x   As Long: x = LastRow(wData)
    
    With Range("J1:J" & x)
        .Value = Evaluate("IF(" & .Address & "," & .Offset(, -5).Address & "&" & .Offset(, -7).Address)
    End With

End Sub
Where I want to fill with contatenated values of two columns (Ex & Cx where x is row number), without looping, but above returns #VALUE .

Please can someone correct?

TIA,
Jack
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How about
Code:
Sub Add_Column_for_Pivot()


    Dim x   As Long: x = lastRow(wData)
    
    With Range("J1:J" & x)
        .Value = Evaluate("IF({1}," & .Offset(, -5).Address & "&" & .Offset(, -7).Address & ")")
    End With


End Sub
 
Upvote 0
Thank you @Fluff and @Marcelo Branco

Marcelo, nice and simple, but I'll be re-using this elsewhere with similar offsets J1:J&x was just arbitary to get some code I could adapt.

Fluff, still trying to use Evaluate more often, I believe it's faster and often shorter than loops or onsheet calcs and understand first part of coercing array with true values (hence {1}), but always seem to get next part wrong... though in this case, seems like I was closer, missed out & ")") - will get there!
 
Upvote 0
I sometimes find it easier to write it like
Evaluate("IF({1},@e&@c)")
so I can see the basic formula construct, then replace the @e & @c with the relevant address
 
Upvote 0
I use that approach alot in general string concatenation, i.e. Range(x).Formula = Replace("=VLOOKUP(item,A1:B@LR,2,0)", "@LR", LR) where LR is the last row number of column B.

Or for new lines Replace(strVariable, "@NL", vbcrlf), for same reason so I can better "see" the basic formula, now I can try it on something new and hopefully use Evaluate more often - awesome, thanks for helping connect the ideas @Fluff!
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top