cell address

Svgmassive

Board Regular
Joined
Nov 2, 2010
Messages
113
I have dates entered in column c.I am trying to get the cell address between the range based on the cell the user selects(active cell).Thanks

Column c
7/4/2011
x
x
x
x
7/5/2011
x
x
x
7/6/2011
x
x
x
x
x
x
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Eg.the date 07/04/2011 cell address is c5 and 07/05/2011 cell address is c11
if the user selects cell let's say c6 the range should be c5-c10 if the user selects cell c11 the range should be c11 and what the cell address is before date 07/06/2011.etc. Hope that explains it
 
Upvote 0
In worksheet module:
Code:
[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] Worksheet_SelectionChange([COLOR="Blue"]ByVal[/COLOR] Target [COLOR="Blue"]As[/COLOR] Range)

    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] rng1 [COLOR="Blue"]As[/COLOR] Range, rng2 [COLOR="Blue"]As[/COLOR] Range
    
    [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] Intersect(Target, Columns("C:C")) [COLOR="Blue"]Then[/COLOR]
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] IsDate(Target) [COLOR="Blue"]Then[/COLOR]
            [COLOR="Blue"]For[/COLOR] i = Target.Row [COLOR="Blue"]To[/COLOR] 1 [COLOR="Blue"]Step[/COLOR] -1
                [COLOR="Blue"]If[/COLOR] IsDate(Cells(i, "C")) [COLOR="Blue"]Then[/COLOR]
                    [COLOR="Blue"]Set[/COLOR] rng1 = Cells(i, "C").Offset(1, 0)
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR]
            [COLOR="Blue"]For[/COLOR] i = Target.Row [COLOR="Blue"]To[/COLOR] Rows.Count
                [COLOR="Blue"]If[/COLOR] IsDate(Cells(i, "C")) [COLOR="Blue"]Then[/COLOR]
                    [COLOR="Blue"]Set[/COLOR] rng2 = Cells(i, "C").Offset(-1, 0)
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR]
            [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] ((rng1 [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR]) [COLOR="Blue"]Or[/COLOR] (rng2 [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR])) [COLOR="Blue"]Then[/COLOR]
                MsgBox "Range address is: " & Range(rng1, rng2).Address
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
sektor thanks a lot i took your idea and ran with it with a little modification it does exactly what i wanted i made it a public function.here's the modification.what you think.

For i = Target.Row To 1 Step -1
If IsDate(Cells(i, Col.Column)) Then
Set rng1 = Cells(i, Col.Column)
Exit For
End If
Next
it will search forward for the previous range until it finds a date
For i = rng1(2).Row To Rows.Count
If Not IsDate(Cells(i, Col.Column)) Then
Set rng2 = Cells(i, Col.Column)
Else
Exit For
End If
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,882
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top