Select a Cell Based on Value of Another Cell VBA

abhi1407

New Member
Joined
Mar 20, 2019
Messages
3
HI - I want to create a column with concatenated column 3 like below. Basically select values from column 1 and column 2 where column 2 value is non zero.

Can anyone please help ? Highly appreciated.

Column1Column2Column3
Apple1010 Apple, 8 Car, 2 Home
Mango0
Car8
Boy0
Home2

<tbody>
</tbody>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi & welcome to MrExcel.
How about
Code:
Function abhi(Rng As Range) As String
   Dim cl As Range
   
   For Each cl In Rng.Columns(2).Cells
      If Not cl = 0 Then
         abhi = abhi & cl.Value & ", " & cl.Offset(, -1).Value & vbLf
      End If
   Next cl
   abhi = Left(abhi, Len(abhi) - 1)
End Function
Used like


Excel 2013/2016
ABC
1Column1Column2Column3
2Apple1010, Apple 8, Car 2, Home
3Mango0
4Car8
5Boy0
6Home2
All
Cell Formulas
RangeFormula
C2=abhi(A2:B6)
 
Upvote 0
Thanks so much for quick response. One last question, how do i change
10, Apple8, Car2, Home to 10 Apple, 8 Car, 2 Home

Hi & welcome to MrExcel.
How about
Code:
Function abhi(Rng As Range) As String
   Dim cl As Range
   
   For Each cl In Rng.Columns(2).Cells
      If Not cl = 0 Then
         abhi = abhi & cl.Value & ", " & cl.Offset(, -1).Value & vbLf
      End If
   Next cl
   abhi = Left(abhi, Len(abhi) - 1)
End Function
Used like

Excel 2013/2016
ABC
1Column1Column2Column3
2Apple1010, Apple
8, Car
2, Home
3Mango0
4Car8
5Boy0
6Home2

<tbody>
</tbody>
All

Worksheet Formulas
CellFormula
C2=abhi(A2:B6)

<tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
use
Code:
Function abhi(Rng As Range) As String
   Dim cl As Range

   For Each cl In Rng.Columns(2).Cells
      If Not cl = 0 Then
         abhi = abhi & cl.Value & ", " & cl.Offset(, -1).Value & ", "
      End If
   Next cl
   abhi = Left(abhi, Len(abhi) - 2)
End Function
 
Upvote 0
Thanks. It works great.

use
Code:
Function abhi(Rng As Range) As String
   Dim cl As Range

   For Each cl In Rng.Columns(2).Cells
      If Not cl = 0 Then
         abhi = abhi & cl.Value & ", " & cl.Offset(, -1).Value & ", "
      End If
   Next cl
   abhi = Left(abhi, Len(abhi) - 2)
End Function
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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