Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2013-06-17
6
324 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

838 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