Extract Values From Cells Based On Corresponding Cell Value

fiberboysa

Board Regular
Joined
Apr 25, 2012
Messages
106
Office Version
  1. 365
Platform
  1. Windows
Hi Dear Allz,
I am trying to extract values from preceding rows based on a value which is given in next columns.

Like in following example, in cell A2 Cell value is 10 (Highlighted in Green) the corresponding value in cell D2 is 3 (Highlighted in Red) so the value required in Cell E2 is combination of following three rows of Cell A2 i.e. values of cells A3, A4 and A5 which are A-1111 B-2222 C-3333 respectively. And in case of Cell A6 the corresponding value in cell D6 is 2 so the required value in cell E6 will be D-4444 E-5555.

The data continuous like this and formula or VBA code will fill my requirement in column E. Kindly let me know if you need further clarification. Thanks in advance... :)

ABCDE
1CLMN 1CLMN 2CLMN 3CLMN 4CLMN 5 (Required Formula/VBA)
2103A-1111 B-2222 C-3333
3A-1111
4B-2222
5C-3333
62024444 5555
74444
85555
9304
10F-6666F-6666 7777 H-8888 9999
117777
12H-8888
139999

<tbody>
</tbody>
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about this UDF
Code:
Function fiberboysa(Cl As Range, Rws As Long) As String
   fiberboysa = Join(Application.Transpose(Cl.Offset(1).Resize(Rws).Value), " ")
End Function
Used like =fiberboysa(A2,D2)
 
Upvote 0
Thanks for reply. I have tried the UDF but its giving me VALUE error where value in column D is 1.
 
Upvote 0
Ok, how about
Code:
Function fiberboysa(Cl As Range, Rws As Long) As String
   If Rws = 1 Then
      fiberboysa = Cl.Offset(1).Value
   Else
      fiberboysa = Join(Application.Transpose(Cl.Offset(1).Resize(Rws).Value), " ")
   End If
End Function
 
Upvote 0
This will cater for 0 or empty in col D
Code:
Function fiberboysa(Cl As Range, Rws As Long) As String
   Select Case Rws
      Case 1
         fiberboysa = Cl.Offset(1).Value
      Case Is > 1
         fiberboysa = Join(Application.Transpose(Cl.Offset(1).Resize(Rws).Value), " ")
      Case Else
         fiberboysa = ""
   End Select
End Function
 
Upvote 0
Solution
This will cater for 0 or empty in col D
Code:
Function fiberboysa(Cl As Range, Rws As Long) As String
   Select Case Rws
      Case 1
         fiberboysa = Cl.Offset(1).Value
      Case Is > 1
         fiberboysa = Join(Application.Transpose(Cl.Offset(1).Resize(Rws).Value), " ")
      Case Else
         fiberboysa = ""
   End Select
End Function
Thanks dear!!! you saved my day. Have a good life ahead :)
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Hi Fluff,
When I was checking my data I found an error. There is one more addition in scenario that it should only take cell value if it starts with a specific character like if cell value starts with "SRL#" then this UDF should take this cell value else it should move to next cell. I hope following table will clarify this further.
ABCDE
1CLMN 1CLMN 2CLMN 3CLMN 4CLMN 5 (Required Formula/VBA)
2103A-1111 B-2222 C-3333
3SLR# A-1111
4SLR# B-2222
5SLR# C-3333
62024444 5555
7SLR# 4444
8SLR# 5555
9304
10SLR# F-6666F-6666 7777 H-8888 9999
11SLR# 7777
12
13SLR# H-8888
14ABCXYZ 12
15SLR# 9999

<tbody>
</tbody>

You can see that after row 11 the next row i.e. row 12 is empty so UDF should ignore this empty row and in row 14 there is text that do not start with SLR# so it should be ignored as well.
 
Last edited:
Upvote 0
How about
Code:
Function fiberboysa(Rng As Range, Rws As Long) As String
   Dim i As Long
   Dim Cl As Range
   
   For Each Cl In Rng
      If Left(Cl, 4) = "SLR#" Then
         fiberboysa = fiberboysa & Cl.Value
         i = i + 1
         If i = Rws Then Exit For
      End If
   Next Cl
   fiberboysa = Trim(Replace(fiberboysa, "SLR#", ""))
End Function
used like
=fiberboysa(A9:A15,D9)
 
Upvote 0
Yes!!! that worked!
My data is usually in10,000+ rows. It took time but it worked. I've also changed the range of Cl to a variable so it just search within range rather 10000 rows :). Thanks again and have a good day!
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,054
Latest member
juliecooper255

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