Solved

SELECTED RANGE

Posted on 2014-10-12
3
92 Views
Last Modified: 2014-10-28
I am trying to get the selected region between dates.File attached
Book1.xlsm
0
Comment
Question by:Svgmassive
  • 2
3 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40375697
Sub GetRange()

Dim lngLastRow As Long
Dim lngRowEnd As Long
Dim lngRowStart As Long

lngLastRow = Range("B1048576").End(xlUp).Row

' Find start date
For lngRowStart = ActiveCell.Row To 1 Step -1
    If IsDate(Cells(lngRowStart, 2)) Then
        Exit For
    End If
Next

' Find end date
For lngRowEnd = ActiveCell.Row + 1 To lngLastRow
    If IsDate(Cells(lngRowEnd, 2)) Then
        lngRowEnd = lngRowEnd - 1
        Exit For
    End If
Next

Range("B" & lngRowStart & ":AF" & lngRowEnd).Select
End sub

Open in new window

0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40380929
I've tweaked Martin's code to pick up all rows if one chooses a cell at or after the last date shown (selects to the last used cell's row)

Sub GetRange()
    Dim lngLastRow As Long
    Dim lngRowEnd As Long
    Dim lngRowStart As Long
    Dim lngLastUsedRow As Long
    
    lngLastRow = Range("B" & Cells.Rows.Count).End(xlUp).Row
    lngLastUsedRow = Cells.SpecialCells(xlLastCell).Row
    
    ' Find start date
    For lngRowStart = ActiveCell.Row To 1 Step -1
        If IsDate(Cells(lngRowStart, 2)) Then
            Exit For
        End If
    Next lngRowStart
    
    ' Find end date
    If ActiveCell.Row + 1 > lngLastRow Then
        lngRowEnd = lngLastUsedRow
    Else
        For lngRowEnd = ActiveCell.Row + 1 To lngLastRow
            If IsDate(Cells(lngRowEnd, 2)) Then
                lngRowEnd = lngRowEnd - 1
                Exit For
            End If
        Next lngRowEnd
    End If
    Range("B" & lngRowStart & ":AF" & lngRowEnd).Select
End Sub

Open in new window



-Glenn
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40409852
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now