Solved

SELECTED RANGE

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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…

777 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