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.

[TABLE="class: grid, width: 20, align: center"]
<tbody>[TR]
[TD]Column1[/TD]
[TD]Column2[/TD]
[TD]Column3[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]10[/TD]
[TD]10 Apple, 8 Car, 2 Home[/TD]
[/TR]
[TR]
[TD]Mango[/TD]
[TD]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Car[/TD]
[TD]8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Boy[/TD]
[TD]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Home[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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
Column1Column2Column3
Apple10, Apple
8, Car
2, Home
Mango
Car
Boy
Home

<tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"]10[/TD]

[TD="align: center"]3[/TD]

[TD="align: right"]0[/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]8[/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]

[TD="align: right"]0[/TD]
[TD="align: right"][/TD]

[TD="align: center"]6[/TD]

[TD="align: right"]2[/TD]
[TD="align: right"][/TD]

</tbody>
All

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]C2[/TH]
[TD="align: left"]=abhi(A2:B6)[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
 
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,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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