Concatenate Cell Range - Empty Cells

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
961
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello,
I have a range of cell from A1 through F1
Values in this range can vary from 00 through 99
Cell G1 contains the formula that concatenates all the values from range A1 through F1
Random cells in A1 through F1 may be empty.
Each of the values will be separated by a coma.

I found the following code for a Functions that works but I'm looking for code that will keep the 1st zero when concatenating the range
If a cell contains any value for 00 though 09 I would like to keep the 1st zero.

Function ConcatenateButBlank(selectArea As Range) As String
For Each oneCell In selectArea: finalResult = IIf(oneCell = "", finalResult & "", finalResult & oneCell & ","): Next
ConcatenateButBlank = Left(finalResult, Len(finalResult) - 1)
End Function

Thank you in advance!!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I think this replacement for the function you are now using should do what you want...
VBA Code:
Function ConcatenateButBlank(selectArea As Range) As String
  ConcatenateButBlank = Replace(Application.Trim(Replace(" " & Join(Evaluate("TEXT(" & selectArea.Address & ",""00"")")) & " ", " 00 ", " ")), " ", ",")
End Function
 
Last edited:
Upvote 0
May be:
VBA Code:
Function ConcatenateButBlank(selectArea As Range) As String
Dim val As String, st As String, onecell As Range
Application.Volatile
For Each onecell In selectArea
    If IsEmpty(onecell) Then
        val = "00"
    Else: val = Format(onecell, "00")
    End If
    If st = "" Then
        st = val
    Else
        st = st & "-" & val
    End If
Next
ConcatenateButBlank = st
End Function
 
Upvote 0
Another one you could also try

VBA Code:
Function ConcatenateButBlank(selectArea As Range) As String
  ConcatenateButBlank = Join(Filter(Evaluate(Replace("if(#="""",""|"",if(len(#)=1,0&#,#))", "#", selectArea.Address)), "|", 0), ",")
End Function
 
Upvote 0
Solution
Thank you everyone for your help
Peter. your code worked.
I did not try Ricks or bebo's suggestions
Sorry for the late thank you to all.
Very much appreciated
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

I did not try Ricks or bebo's suggestions
😎 That may not encourage them to try to help you if you have further questions.
Remember that people are contributing their time to try to help you for no payment. ;)
 
Upvote 0
Ricks code returns "VALUES" in the cell
bebo's code works but adds dashes for blanks cells
Peter. Your code works. The only question is when I reopen the Excel file I need to go into each cell and select the Enter key. Is there a way this Function will work when I reopen the Excel file?
Thank you all again
Sorry I did not respond to all the help.

01--07-12--27
01071227
 
Upvote 0
The only question is when I reopen the Excel file I need to go into each cell and select the Enter key.
Are you sure that your 'Calculation' is not set to Manual? Have a look at the Formulas ribbon tab -> Calculation section -> Calculation Options

1658893450800.png
 
Upvote 0
Ricks code returns "VALUES" in the cell
bebo's code works but adds dashes for blanks cells
Peter. Your code works. The only question is when I reopen the Excel file I need to go into each cell and select the Enter key. Is there a way this Function will work when I reopen the Excel file?
Thank you all again
Sorry I did not respond to all the help.

01--07-12--27
01071227
With this lines of code:
VBA Code:
 If IsEmpty(onecell) Then
        val = "00"
So supprise if it did not work!

Capture.JPG
 
Upvote 0

Forum statistics

Threads
1,215,149
Messages
6,123,311
Members
449,095
Latest member
Chestertim

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