Listing all values within a range of values

arundharmarajan

New Member
Joined
Jun 12, 2018
Messages
6
Hi,

I have a column(assume A) with various numerical values from 0000 to 9000 (not in any incremental sequence).

There is another column which has many ranges, like 1794-6317 in it (the lower and upper values will be valid and will be in the list).

Is there any way to display the numbers within that range by looking at column A?

I am fine if the output comes in multiple cells or in a single cell separated by comma or any other delimiter.

I have given a sample image to explain better.

28UDK4x.jpg
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Sub FT()
Dim rng1 As Range, rng2 As Range, cel As Range, cel2 As Range, s$, l%, r%
Set rng1 = Range([A1], Cells(Rows.Count, "A").End(xlUp))
Set rng2 = Range([c1], Cells(Rows.Count, "C").End(xlUp))
rng2.Offset(0, 2).NumberFormat = "@"
For Each cel2 In rng2
    s = ""
    l = Left(cel2, 4) - 1
    r = Right(cel2, 4) + 1
    For Each cel In rng1
        If cel > l And cel < r Then
            If s = "" Then
                s = cel
            Else
                s = s & "," & cel
            End If
        End If
    Next
    cel2(1, 3) = s
Next
End Sub
 
Upvote 0
Here is a UDF (user defined function) that you can try...
Code:
[table="width: 500"]
[tr]
	[td]Function WithinSpan(CellsToCheck As Range, DashedSpan As String) As String
  Dim SpanLimits As Variant
  SpanLimits = Split(DashedSpan, "-")
  WithinSpan = Replace(Application.Trim(Join(Application.Transpose(Evaluate(Replace("IF((0+@>=" & SpanLimits(0) & ")*(0+@<=" & SpanLimits(1) & "),@,"""")", "@", CellsToCheck.Address(0, 0)))))), " ", ", ")
End Function[/td]
[/tr]
[/table]

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use WithinSpan just like it was a built-in Excel function. For example, for the example you posted, put this formula in cell E1 and copy it down to the end of the number spans you have in Column C...

=WithinSpan(A1:A11,C1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Code:
Sub FT()
Dim rng1 As Range, rng2 As Range, cel As Range, cel2 As Range, s$, l%, r%
Set rng1 = Range([A1], Cells(Rows.Count, "A").End(xlUp))
Set rng2 = Range([c1], Cells(Rows.Count, "C").End(xlUp))
rng2.Offset(0, 2).NumberFormat = "@"
For Each cel2 In rng2
    s = ""
    l = Left(cel2, 4) - 1
    r = Right(cel2, 4) + 1
    For Each cel In rng1
        If cel > l And cel < r Then
            If s = "" Then
                s = cel
            Else
                s = s & "," & cel
            End If
        End If
    Next
    cel2(1, 3) = s
Next
End Sub

Here is a UDF (user defined function) that you can try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function WithinSpan(CellsToCheck As Range, DashedSpan As String) As String
  Dim SpanLimits As Variant
  SpanLimits = Split(DashedSpan, "-")
  WithinSpan = Replace(Application.Trim(Join(Application.Transpose(Evaluate(Replace("IF((0+@>=" & SpanLimits(0) & ")*(0+@<=" & SpanLimits(1) & "),@,"""")", "@", CellsToCheck.Address(0, 0)))))), " ", ", ")
End Function[/TD]
[/TR]
</tbody>[/TABLE]

Thank you for the responses. If I have to increase the complexity of the issue, if the input has multiple ranges and also single values and I want them encompassed within a tag, will it be possible?

Something of this sort..

ape4IDk.jpg


P.S. Sorry, unable to find a way to attach a sample file to make it easier.
 
Upvote 0
Welcome to the MrExcel board!

.. if the input has multiple ranges and also single values and I want them encompassed within a tag, will it be possible?
I'm assuming that "1000" in cell F2 instead of "0000" was a typo.

See if this is what you want

Code:
Sub ListValues()
  Dim d As Object
  Dim a As Variant, bits As Variant
  Dim i As Long, j As Long, k As Long
  Dim s As String
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  For Each bits In a
    d(bits) = 1
  Next bits
  a = Range("C2", Range("C" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    s = vbNullString
    bits = Split(a(i, 1), ",")
    For j = 0 To UBound(bits)
      If InStr(1, bits(j), "-") = 0 Then
        If d.exists(bits(j)) Then s = s & "</a><a>" & bits(j)
      Else
        For k = Split(bits(j), "-")(0) To Split(bits(j), "-")(1)
          If d.exists(Format(k, "0000")) Then s = s & "</a><a>" & Format(k, "0000")
        Next k
      End If
    Next j
    a(i, 1) = Mid(s, 5) & "</a>"
  Next i
  With Range("F2").Resize(UBound(a))
    .NumberFormat = "@"
    .Value = a
    .Columns.AutoFit
  End With
End Sub

My results:
(My signature block below has a link with options for posts screen shots/sample data like this.)


Book1
ABCDEF
1
200000000,1794-631700001794318659736317
300871076-7635,83691076179431865973631776358369
410761794,5973-7912,9000179459736317763579129000
517940000-1076,6317-7912000000871076631776357912
6318610761076
75973
86317
97635
107912
118369
129000
Check Values
 
Last edited:
Upvote 0
Welcome to the MrExcel board!

I'm assuming that "1000" in cell F2 instead of "0000" was a typo.

See if this is what you want

Oops, yeah. Missing out on F2 was a mistake. Thank you so much for the response. Works beautifully!!!!
 
Last edited by a moderator:
Upvote 0
Thank you so much for the response. Works beautifully!!!!
You are welcome. :)

BTW, best not to fully quote long posts as it makes the thread harder to read/navigate and just occupies storage space needlessly. If you want to quote, quote small, relevant parts only, like I've edited your post to.
 
Last edited:
Upvote 0
Here is how I would rewrite the UDF that I posted in Message #3 to meet your new requirements...
Code:
[table="width: 500"]
[tr]
	[td]Function WithinSpan(CellsToCheck As Range, DashedSpan As String) As String
  Dim V As Variant, SpanLimits As Variant
  For Each V In Split(DashedSpan, ",")
    SpanLimits = Split(V & "-" & V, "-")
    WithinSpan = WithinSpan & "<a>" & Replace(Application.Trim(Join(Application.Transpose(Evaluate(Replace("IF((0+@>=" & SpanLimits(0) & ")*(0+@<=" & SpanLimits(1) & "),@,"""")", "@", CellsToCheck.Address(0, 0)))))), " ", "</a><a>") & "</a>"
  Next
End Function[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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