Find row of found value - VBA

yomomo

New Member
Joined
Mar 8, 2018
Messages
11
So i'm trying to find the row number of the value "period" and i don't know how to get the correct row number in which the value of "period" is in. Right now the value of "period" is taken as the row number, so when the msgbox pops up it shows the wrong information.
This is what my sub looks like:

Code:
Sub Oldest()    
    Dim ENROLL_PERIOD As Range
    Dim period As Integer
    
    Set ENROLL_PERIOD = Range("E2:E" & Rows.Count)
    period = Application.WorksheetFunction.Min(ENROLL_PERIOD)
    If Val(period) And 1 Then
    MsgBox "Enrolled: Spring Semester " & (period / 2 + 1949) & vbNewLine & "Student Id: " & (Cells(period, 12)) & vbNewLine & "Enroll Date: " & (Cells(period, 4)) & vbNewLine & "Program Type: " & (Cells(period, 11))
    Else
    MsgBox "Enrolled: Fall Semester " & (period / 2 + 1949) & vbNewLine & "Student Id: " & (Cells(period, 12)) & vbNewLine & "Enroll Date: " & (Cells(period, 4)) & vbNewLine & "Program Type: " & (Cells(period, 11))
    End If



End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Perhaps.
Code:
Sub Oldest()    
Dim ENROLL_PERIOD As Range
Dim period As Integer
Dim Res As Variant

    Set ENROLL_PERIOD = Range("E2:E" & Rows.Count)

    period = Application.WorksheetFunction.Min(ENROLL_PERIOD)

    Res = Application.Match(period, ENROLL_PERIOD, 0)

    If Not IsError(Res) Then
        MsgBox "Enrolled: Spring Semester " & (period / 2 + 1949) & vbNewLine & "Student Id: " & Cells(Res+1, 12) & vbNewLine & "Enroll Date: " & Cells(Res+1, 4) & vbNewLine & "Program Type: " & Cells(Res+1, 11)    
    End If

End Sub
 
Upvote 0
Thanks for your reply but i actually found another way :).

Code:
 Sub Oldest()    
    Dim ENROLL_PERIOD As Range
    Dim period As Integer
    Dim minPeriodRow As Long
    
    Set ENROLL_PERIOD = Range("E2:E" & Rows.Count)
    period = Application.WorksheetFunction.Min(ENROLL_PERIOD)
    minPeriodRow = ENROLL_PERIOD.Find(what:=period, lookat:=xlWhole, LookIn:=xlValues).Row
    If Val(period) And 1 Then
    MsgBox "Enrolled: Spring Semester " & (period / 2 + 1949) & vbNewLine & "Student Id: " & (Cells(minPeriodRow, 12)) & vbNewLine & "Enroll Date: " & (Cells(minPeriodRow, 4)) & vbNewLine & "Program Type: " & (Cells(minPeriodRow, 11))
    Else
    MsgBox "Enrolled: Fall Semester " & (period / 2 + 1949) & vbNewLine & "Student Id: " & (Cells(minPeriodRow, 12)) & vbNewLine & "Enroll Date: " & (Cells(minPeriodRow, 4)) & vbNewLine & "Program Type: " & (Cells(minPeriodRow, 11))
    End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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