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:
Another option if you're interested
Code:
Function FryGirl2(Rng As Range) As String
   Dim Cl As Range
   For Each Cl In Rng
      If Cl.Value <> "" Then
         FryGirl2 = FryGirl2 & Cl.Value & "(" & Cl.Offset(, -1).Value & ")" & " + "
      End If
   Next Cl
   FryGirl2 = Replace(Left(FryGirl2, Len(FryGirl2) - 3), "(a)", "")
End Function
Used like
=FryGirl2(B1:B11)
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Thank you Fluff. This is awesome as well. I actually misspoke where the a-value is. The a-value is in B11 and the data actually starts in B1:B10.

Expected result: 112 + 15.34(x1)

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

<tbody>
</tbody>
 
Last edited:
Upvote 0
I think I figured it out using Fluff's sample. I added the IF statement and added a Rng2.

Code:
[COLOR=#000000]FunctionFryGirl3(Rng As Range, Rng2 As Range) As String[/COLOR]
[COLOR=#000000]    Dim Cl As Range[/COLOR]
[COLOR=#000000]    For Each Cl In Rng[/COLOR]
[COLOR=#000000]        If Cl.Value <> "" Then[/COLOR]
[COLOR=#000000]            FryGirl3 = FryGirl3 &"(" & Cl.Value & Cl.Offset(, -5).Value & ")"& " + "[/COLOR]
[COLOR=#000000]        End If[/COLOR]
[COLOR=#000000]    Next Cl[/COLOR]
[COLOR=#000000]    If Rng2.Value <> "" Then[/COLOR]
[COLOR=#000000]        FryGirl3 = "Y = [" &Rng2.Value & " + " & Replace(Left(FryGirl3, Len(FryGirl3) -3), "(a)", "") & "](IAF)"[/COLOR]
[COLOR=#000000]    Else[/COLOR]
[COLOR=#000000]        FryGirl3 = "Y = [" &Replace(Left(FryGirl3, Len(FryGirl3) - 3), "(a)", "") &"](IAF)"[/COLOR]
[COLOR=#000000]    End If[/COLOR]
[COLOR=#000000]End Function[/COLOR]
 
Last edited:
Upvote 0
Glad you sorted it out & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,096
Messages
6,128,809
Members
449,468
Latest member
AGreen17

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