+ Reply to Thread
Results 1 to 5 of 5

VBA to find the range at the intersection of two values.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    63

    VBA to find the range at the intersection of two values.

    Good Day all,

    The problem I'm having today is that I would like a VBA module that will allow me to choose a date, and it will then select the cell that intersects between the two dates that are listed in the rows and columns. I've attached an example worksheet for reference that can explain the problem better.


    Thanks for the help.
    Chris
    Attached Files Attached Files
    Last edited by Benisato; 01-31-2013 at 03:41 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA to find the range at the intersection of two values.

    how do you want choose the date ? selecting a cell ?
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    63

    Re: VBA to find the range at the intersection of two values.

    The Date will be in a cell on a different sheet.

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA to find the range at the intersection of two values.

    Try:

    Sub Date_Intersection()
    Dim ws As Worksheet:    Set ws = Sheets("Sheet1")
    Dim myInput As Variant
    Dim myDate As String
    Dim iCol As Range, iRow As Range
    Dim lastrow As Long, lastcol As Long
    
    lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
    lastcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
    
    myInput = Application.InputBox("Please enter the date.")
    
    If myInput = "" Then Exit Sub
    If myInput = False Then Exit Sub
    
    If IsDate(myInput) Then
        myDate = Format(DateValue(myInput), "d-mmm")
    Else
        MsgBox "Please enter a valid date."
        Exit Sub
    End If
    
    Set iCol = ws.Range(Cells(1, 1), Cells(1, lastcol)).Find(What:=myDate, LookIn:=xlValues, LookAt:=xlWhole)
    If iCol Is Nothing Then
        MsgBox ("The date " & myDate & " could not be found.")
        Exit Sub
    End If
    
    Set iRow = ws.Range("A1:A" & lastrow).Find(What:=myDate, LookIn:=xlValues, LookAt:=xlWhole)
    
    If iRow Is Nothing Then
        MsgBox ("The date " & myDate & " could not be found.")
        Exit Sub
    End If
    
    ws.Cells(iRow.Row, iCol.Column).Select 'this is where you asked to select it
    
    End Sub

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA to find the range at the intersection of two values.

    Sub a()
    Dim c As Range, sdate As Date
    sdate = Sheets(2).Range("A14").Value
    With Sheets(1).UsedRange
      Set c = .Find(what:=sdate, LookIn:=xlFormulas)
      col = c.Column
      Set c = .FindNext(c)
      crow = c.Row
    End With
    MsgBox Cells(crow, col).Text
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1