Solved

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

Posted on 2013-06-17
6
295 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
Comment Utility
If cel.Value <> "" Then strcat = strcat & IIf(strcat = "", "", dlm) & IIf(bUseFormat, cel.Text, cel.Value)
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
Comment Utility
EEEEE   Excellent Experts-Exchange Excel Experts
0
 

Author Comment

by:Steve_Brady
Comment Utility
One additional note:

The custom delimiter is interestingly not included when all of the cells in the defined range are empty.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Steve_Brady
Comment Utility
>>EEEEE   Excellent Experts-Exchange Excel Experts

Touché  :)
0
 

Author Comment

by:Steve_Brady
Comment Utility
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
Comment Utility
>>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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

728 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

13 Experts available now in Live!

Get 1:1 Help Now