Solved

Excel VBA code needs a small (I hope) tweak please

Posted on 2013-06-17
6
315 Views
Last Modified: 2013-06-17
Hello,

Sometime ago, I received a VBA solution from some "EEEE"*

        Concatenate in Excel without entering every cell reference

for the purpose of 1) concatenating the contents of multiple cells when the number of involved cells is too great to practically use the common concatenating Excel formula:

        =A1&A2&A3&A4&...etc.

and 2) being able to select a custom delimiter.

The code in the solution is as follows:

Function strcat(rng As Range, Optional dlm As String = ",", Optional bUseFormat As Boolean = True)
Dim cel As Range
For Each cel In rng
If cel.Value <> "" Then strcat = strcat & dlm & IIf(bUseFormat, cel.Text, cel.Value)
Next cel
strcat = Mid$(strcat, Len(dlm))
End Function

Open in new window

and the function to enter into the spreadsheet is here:

=strcat(Desired_Range,Custom_Delimiter)

Open in new window

Something I noticed just recently that I had not noticed previously is that when using this code, a delimiter is inserted prior to the first cell's contents. For example, if the contents of five cells to be concatenated are:

        dog
        cat
        bird
        fish
        iguana

respectively and the custom delimiter is a closing curly bracket "}", the result would be displayed as follows:

        }dog}cat}bird}fish}iguana

with the first character being a delimiter.

Could someone tweak the above code so that this initial delimiter is not included in the result display?

Thanks


* EEEE = Excellent Experts-Exchange Experts
0
Comment
Question by:Steve_Brady
  • 4
  • 2
6 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39254764
If cel.Value <> "" Then strcat = strcat & IIf(strcat = "", "", dlm) & IIf(bUseFormat, cel.Text, cel.Value)
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39254765
EEEEE   Excellent Experts-Exchange Excel Experts
0
 

Author Comment

by:Steve_Brady
ID: 39254770
One additional note:

The custom delimiter is interestingly not included when all of the cells in the defined range are empty.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Steve_Brady
ID: 39254774
>>EEEEE   Excellent Experts-Exchange Excel Experts

Touché  :)
0
 

Author Comment

by:Steve_Brady
ID: 39254873
Correction:

The thread title & link included in the original question above is incorrect. The correct title & link is the following:

        Abbreviation to shorten lengthy repetitive Excel formulas
0
 

Author Comment

by:Steve_Brady
ID: 39254881
>>ssaiqbh

If cel.Value <> "" Then strcat = strcat & IIf(strcat = "", "", dlm) & IIf(bUseFormat, cel.Text, cel.Value)


Thanks Syed—just what I needed. Of course you would know, since you wrote the original!  :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now