Solved

SELECTED RANGE

Posted on 2014-10-12
3
97 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

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 48

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

691 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