Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-06-17
6
Medium Priority
?
357 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

810 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