Help requested to find VBA solution to build text string from two columns of data

shstrating

Board Regular
Joined
Sep 8, 2009
Messages
61
Office Version
  1. 365
Platform
  1. Windows
Win7 Pro, SP1; Excel 2016

I am trying to find a VBA solution to allow me to build a single text string from two columns of data.

The number of entries in the two columns is the same, but the number of rows of entries will vary.

The two columns of data will be manually copied as a range from a source worksheet to a destination worksheet in the same workbook.

The upper left cell into which the range is copied on the destination worksheet will be a constant (B3).

The resulting single text string should be “B3 – C3, B4 – C4, B5 – C5, and so on” until the end of the data is reached.

Here's a sample showing the results for the first 3 rows:
BCD
1No. of rows
29
3111Jet Black111 - Jet Black, 114 - Folkstone Grey, 115 - Medium Grey
4114Folkstone Grey
5115Medium Grey
6117Soft Grey
7118Bright White
8119Pumice
9121Micro Grey
10128Fog
11129Micro Sand

<tbody>
</tbody>
Sheet1
If I do this formulaically and manually, it looks like this:
=TEXTJOIN(", ",TRUE,TEXTJOIN(" - ",TRUE,B3:C3),TEXTJOIN(" - ",TRUE,B4:C4),TEXTJOIN(" - ",TRUE,B5:C5))

Problem with that is that the end user has to either first TEXTJOIN the contents of Cols B & C one row at a time in a helper column and then TEXTJOIN the resulting helper column into a blank cell, or they have to construct a formula similar to mine above to do it all in a blank cell. Either way it won’t get used if they have to jump through these hoops. Most of the end users are not skilled or patient enough in Excel for this.

If possible I want to automate it so that the only thing the end user has to do is Copy/Paste the range of data starting in cell B3 and then click a Command Button to have the text string built in cell D3.

I have almost no VBA knowledge/experience, so I have been scouring this group and the web for bits of code that I might be able to cobble together into a solution, but because I don’t know the VBA language (and I guess my search strings are crappy) I am hitting a brick wall.

The one thing I’ve been able to do is to find code that will find the last row of data in the range, and then use that value to determine the total number of rows of data, under the assumption that I will need this as part of a LOOP or FOR/NEXT or other counter of some kind.
Code:
Sub findLastCell()


    Dim rowcount As Long
    Dim lastRow As Long
    
    lastRow = Cells(Rows.Count, 2).End(xlUp).Row
        
    rowcount = lastRow - 2
    
    Range("C2") = rowcount
    
End Sub

Any help anyone can give me would be much appreciated.

Steve
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Use a table, put your data in columns 1 and 2, in column 3 use helper formula
Code:
=TEXTJOIN("-",TRUE,[@Column1],[@Column2])
Then for your output cell, use formula
Code:
=TEXTJOIN(",",TRUE,Table1[Column3])
Then users can just copy/paste into the table.
no code or macros required.
 
Upvote 0
sstiebinger,
Thank you for that straightforward solution.
I will try it out on one or two end users and see what kind of feedback I get.


For the Forum at large,
I would still like to find a VBA solution for two reasons:
1) I'm not allowed to protect this workbook and I'm afraid that what will regularly happen is that end users will accidentally overwrite/erase cell formulas, and
2) I want to use this as a learning experience to increase my VBA knowledge.

Any VBA solutions to be had?

Thanks,
Steve
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,576
Members
449,173
Latest member
Kon123

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