Solved

Find the row for a input

Posted on 2015-01-26
5
64 Views
Last Modified: 2016-02-10
How to write a VBA such that when a user input a given date, it will return the row number that are within the From & To date in the attached worksheet.

Tks
Test.xlsm
0
Comment
Question by:AXISHK
  • 3
5 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 150 total points
ID: 40570415
If the date is in cell D3, then the answer is

=MATCH(D3,A4:A78,1)+ROW(D3)
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40570457
Add this code to a module.

Sub FindRow()

Dim lngRow As Long
Dim dteFind As Date

dteFind = InputBox("Please enter the date")
With ActiveSheet
    For lngRow = 4 To .UsedRange.Rows.Count
        If dteFind <= .Cells(lngRow, 2) Then
            MsgBox "Found in row " & lngRow
            Exit Sub
        End If
    Next
End With
MsgBox "Date " & dteFind & " not found"
End Sub

Open in new window

0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 350 total points
ID: 40570482
Or you can return the row using a Function like this. The function will return row 0 if the date isn't found.

Usage:
Dim dte As Date

dte = "2/18/14"
MsgBox FindRow(dte)

Open in new window


Private Function FindRow(dteFind As Date) As Long

Dim lngRow As Long

With ActiveSheet
    For lngRow = 4 To .UsedRange.Rows.Count
        If dteFind <= .Cells(lngRow, 2) Then
            FindRow = lngRow
            Exit Function
        End If
    Next
End With
FindRow = 0
End Function

Open in new window

0
 

Author Closing Comment

by:AXISHK
ID: 40597731
Tks
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40598914
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question