Solved

SELECTED RANGE

Posted on 2014-10-12
3
90 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 45

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 45

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

746 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

11 Experts available now in Live!

Get 1:1 Help Now