Find Number smallest and largest between a group of numbers that are in a 2d range of cells.

anilsharaf

New Member
Joined
Apr 8, 2014
Messages
43
Office Version
  1. 2007
Platform
  1. Windows
My question is in the last Line of the Post. Below 2nd Table is a sitting arrangement of Examination. I have sitting in 4 rooms for 2 classes. Total Number of students and their Roll Numbers are as follows:
ClassRoll No fromRoll No ToTotal Students
9A9101915959
9B9201924949
11a111011112020
11b112011122626
11c113011132525

Now This is the Sitting Table:
0Colm 1Colm 2Colm 3Colm 4Colm 5
Row 59216112259221113049226
Row 49215112249220113039225
Row 39214112239219113029224
Row 29213112229218113019223
EntranceRow 19212112219217112269222
Abstract :
ClassFromToTotal
9B9212922615
11B112211122606
11C113011130404
Total25

Please suggest me any Function or UDF or Sub to detect the Numbers that are in Abstract box. They are to be detected from the Sitting Table.
Thanks in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Code for macro:
VBA Code:
Sub GetAbstract()
Dim rstrng As Range, sitrng As Range
Dim A, T&, Ta&, K&, X&, Tot&, stno&, ndno&

A = Range("A2:C6")
Set sitrng = Range("C10:G14")
Set rstrng = Range("A18")
ReDim R(1 To UBound(A, 1), 1 To 4)

For T = 1 To UBound(A, 1)
stno = 0: ndno = 0
    For Ta = A(T, 2) To A(T, 3)
    K = WorksheetFunction.CountIf(sitrng, Ta)
    If K > 0 Then
        If stno = 0 Then stno = Ta
    Else
    If stno > 0 Then ndno = Ta - 1: Exit For
    End If
    Next Ta
If stno > 0 Then
ndno = Ta - 1
X = X + 1: R(X, 1) = A(T, 1): R(X, 2) = stno: R(X, 3) = ndno: R(X, 4) = ndno - stno + 1
Tot = Tot + R(X, 4)
End If

Next T
With rstrng
.CurrentRegion.Clear
.Resize(1, 4) = Array("Class", "From", "To", "Total")
.Offset(1, 0).Resize(X, 4) = R
.Offset(X + 1, 0).Resize(1, 4) = Array("Total", "", "", Tot)
.CurrentRegion.Borders.LineStyle = xlContinuous
End With
End Sub

Range A18:D22 is macro result.

1710841563305.png
 
Upvote 1
Solution
Code for macro:
VBA Code:
Sub GetAbstract()
Dim rstrng As Range, sitrng As Range
Dim A, T&, Ta&, K&, X&, Tot&, stno&, ndno&

A = Range("A2:C6")
Set sitrng = Range("C10:G14")
Set rstrng = Range("A18")
ReDim R(1 To UBound(A, 1), 1 To 4)

For T = 1 To UBound(A, 1)
stno = 0: ndno = 0
    For Ta = A(T, 2) To A(T, 3)
    K = WorksheetFunction.CountIf(sitrng, Ta)
    If K > 0 Then
        If stno = 0 Then stno = Ta
    Else
    If stno > 0 Then ndno = Ta - 1: Exit For
    End If
    Next Ta
If stno > 0 Then
ndno = Ta - 1
X = X + 1: R(X, 1) = A(T, 1): R(X, 2) = stno: R(X, 3) = ndno: R(X, 4) = ndno - stno + 1
Tot = Tot + R(X, 4)
End If

Next T
With rstrng
.CurrentRegion.Clear
.Resize(1, 4) = Array("Class", "From", "To", "Total")
.Offset(1, 0).Resize(X, 4) = R
.Offset(X + 1, 0).Resize(1, 4) = Array("Total", "", "", Tot)
.CurrentRegion.Borders.LineStyle = xlContinuous
End With
End Sub

Range A18:D22 is macro result.

View attachment 108597
Thank You very much It worked.
 
Upvote 0
Thank You Very much It worked. But What if I want to supply the following ranges through an Input Box:
A = Range("A2:C6")
Set sitrng = Range("C10:G14")
Set rstrng = Range("A18")
In Other room sitting ranges are different.
 
Upvote 0
This Worked using Input Box To Supply Ranges:
Sub GetAbstractUsingInputBox()
Dim rstrng As Range, sitrng As Range
Dim A, T&, Ta&, K&, X&, Tot&, stno&, ndno&

A = Application.InputBox( _
Title:="Select the 3 Colm GOSWARA", _
Prompt:="Select the range in which Your 3 Colm Roll No GosWara is", _
Type:=8)
Set sitrng = Application.InputBox( _
Title:="Select only Roll Nos That are in Sitting Table", _
Prompt:="Select the Roll Nos in Room Sitting", _
Type:=8)
Set rstrng = Application.InputBox( _
Title:="Select One Cell where Abstract is to be written", _
Prompt:="Select only one cell", _
Type:=8)
ReDim R(1 To UBound(A, 1), 1 To 4)

For T = 1 To UBound(A, 1)
stno = 0: ndno = 0
For Ta = A(T, 2) To A(T, 3)
K = WorksheetFunction.CountIf(sitrng, Ta)
If K > 0 Then
If stno = 0 Then stno = Ta
Else
If stno > 0 Then ndno = Ta - 1: Exit For
End If
Next Ta
If stno > 0 Then
ndno = Ta - 1
X = X + 1: R(X, 1) = A(T, 1): R(X, 2) = stno: R(X, 3) = ndno: R(X, 4) = ndno - stno + 1
Tot = Tot + R(X, 4)
End If

Next T
With rstrng
.CurrentRegion.Clear
.Resize(1, 4) = Array("Class", "From", "To", "Total")
.Offset(1, 0).Resize(X, 4) = R
.Offset(X + 1, 0).Resize(1, 4) = Array("Total", "", "", Tot)
.CurrentRegion.Borders.LineStyle = xlContinuous
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

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