Need to modify TextJoin code to populate not just the top row cell, but all the way down

CaptainKen

New Member
Joined
Oct 31, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I got TextJoin to work in a cell and within code as shown below. What I need to do is to go to the next step, which would be populate the rest of the rows in column C based on values in columns A and B. The number of rows is an unknown. I assume I'd need a Loop or something, but I'm not that knowledgeable.

Column A: Movie name (300 Collection)
Column B: Movie path (movie sets\300 Collection)
Column C: Results populated in this column

This is an example of the desired output:
xcopy "movie sets\300 Collection\*.jpg" "v:\300 Collection" /i

This works from within a cell:
=TEXTJOIN("",TRUE, "xcopy """, "m:\", B1, "\*.jpg"" ""v:\", A1, """ /i")

This works as VBA, although is inserts a single quote as the first character
VBA Code:
Dim strResult As Sring
Dim Sep As String

'Sep represents one double quotation mark
Sep = """"
'MsgBox Sep & "TEXT HERE" & Sep & Range("B1").Value & Sep

strResult = WorksheetFunction.TextJoin("", True, "xcopy ", Sep, "m:\", Range("B1").Value, "\*.jpg", Sep, Space(1), Sep, "v:\", Range("A1").Value, Sep, "/i")
MsgBox strResult
Range("C1").Value = strResult
 
Every piece of code you've supplied works the same, which is perfect.
Not sure who you are referring to there but I only provided one piece of code. :)

BTW, a few comments about using TEXTJOIN for this task
  • CONCAT is a simpler function than using TEXTJOIN with "" as the delimiter
  • vba has its own version of CONCAT (Join function) so if you are going to cycle through the data a row at a time it makes more sense to use vba's native function rather than the worksheet functions (CONCAT or TEXTJOIN)
  • In vba you can also just concatenate text with an ampersand
So here is your original TEXTJOIN code line and options using the point noted above, each a little shorter than the last

VBA Code:
strResult = WorksheetFunction.TextJoin("", True, "xcopy ", Sep, "m:\", Range("B1").Value, "\*.jpg", Sep, Space(1), Sep, "v:\", Range("A1").Value, Sep, "/i")
strResult = WorksheetFunction.Concat("xcopy ", Sep, "m:\", Range("B1").Value, "\*.jpg", Sep, Space(1), Sep, "v:\", Range("A1").Value, Sep, "/i")
strResult = Join(Array("xcopy ", Sep, "m:\", Range("B1").Value, "\*.jpg", Sep, Space(1), Sep, "v:\", Range("A1").Value, Sep, "/i"), "")
strResult = "xcopy " & Sep & "m:\" & Range("B1").Value & "\*.jpg" & Sep & Space(1) & Sep & "v:\" & Range("A1").Value & Sep & "/i"
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Apologies Peter, I missed that it wasn't Robert responding to each post. Both yours and Roberts worked great.

Thanks Peter for providing the multiple examples of how to achieve the same results.

I started with CONCAT, but then learned somewhere that when mixing in text and delimiters that it most likely wouldn't work, so TextJoin. I tried finding the site I read it at again to show you, but no luck.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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