Build string from cells with ignoring blank

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I’m trying to build a text string out of some cell, but Idon’t want the blank cells to appear.

In the first table I have an a-value and two x-values. Pretty easy to create the sting.


123.45 + 15.34(x1) + 12.44(x2)

=B1&" +"&B2&"("&A2&") +"&B3&"("&A3&")"

Data Range

A​
B​
1​
a​
123.45​
2​
x1​
15.34​
3​
x2​
12.44​

<tbody>
</tbody>

In the second table there is only one x-value. Is there a way to ignore the blank cells andjust get the one x-value?

15.34(x1)

Data Range
A​
B​
1​
a​
2​
x1​
15.34​
3​
x2​

<tbody>
</tbody>

 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Is this what you were after? Assuming you have the textjoin function available:

=TEXTJOIN(" + ", TRUE, IF(B1:B3<> "", B1:B3, ""))

This is an array so entered with Ctrl + Shift + Enter.
 
Upvote 0
No I do not have textjoin nor can I get it.
 
Upvote 0
How about this as a UDF?

Code:
Function FryGirl(rng As Range)


Dim cell As Range
Dim text As String


For Each cell In rng
    If cell.text <> "" Then
        text = text & cell.text & " + "
    End If
Next cell


If Right(text, 3) = " + " Then
    text = Left(text, Len(text) - 3)
End If


FryGirl = text


End Function

Then entered the formula as:

=fryGirl(B1:B3)
 
Upvote 0
That works great to return

123.45 + 15.34 + 12.44

but can it take into account the x-values also?

123.45 + 15.34(x1) + 12.44(x2)
 
Upvote 0
Out of curiosity is it just going to be those 3 rows or is your actual data longer?

Also do you want the (a) to appear for the first row?
 
Last edited:
Upvote 0
It could be up to 10 rows and there may not be an a-value at times. It could just be x1 all by itself. 15.34(x1)

Again, there may not be an a-value, but it will always start with x1.

Data Range
A​
B​
1​
a​
123.45​
2​
x1​
15.34​
3​
x2​
12.44​
4​
x3​
5​
x4​
6​
x5​
7​
x6​
8​
x7​
9​
x8​
10​
x9​
11​
x10​

<tbody>
</tbody>
 
Last edited:
Upvote 0
Ok how does this work for you?

Code:
Function FryGirl2(rng As Range, rng2 As Range)


Dim cell As Range
Dim text As String
Dim count As Integer


For Each cell In rng
count = count + 1
    If cell.text <> "" Then
        text = text & cell.text
        If Left(rng2(count), 1) = "x" Then
            text = text & "(" & rng2(count) & ")"
        End If
        text = text & " + "
    End If
Next cell


If Right(text, 3) = " + " Then
    text = Left(text, Len(text) - 3)
End If


FryGirl2 = text


End Function


and formula would be:

=fryGirl2(B1:B3, A1:A3)
 
Upvote 0
That is simply awesome. Thank you so much for your time.
 
Upvote 0

Forum statistics

Threads
1,215,470
Messages
6,124,992
Members
449,201
Latest member
Lunzwe73

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