Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to I return the position of a selected cell in a range?

Hi all,

I am trying to create a routine in VBA using which I should be able to
do the following:

- Lets say I have 10 column headers
- They have long descripotions (e.g. "Time spent on.....") but a short
header name (e.g. TM1).
- The descriptions in the corresponding columns in a different row
(hidden)

I was to be able to display a comment with the appropriate text
whenever the user clicks on a header cell.

I am half way there. Code attached: What I can't figure out is how to
return the position of the cell that the user selects so that I can
refer to the right text for the comment. Any help is highly
appreciated.

Code-

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim newtext$
Dim Addr$
Dim RangeToCheck As Range
Dim RangeComment As Range
Dim isect As Range
Dim intRow As Integer
Dim intCol As Integer

If Target.Cells.Count 1 Then
Exit Sub

Set RangeToCheck = ThisWorkbook.Sheets("Rep Data").Range("headers")
RangeComment = ThisWorkbook.Sheets("Rep Data").Range("headertext")
Set isect = Application.Intersect(Target, RangeToCheck)

If isect Is Nothing Then Exit Sub
Addr = isect.Address
'intCol = isect.Row
ThisWorkbook.Sheets("Rep Data").Range("row").Value = Addr
'ThisWorkbook.Sheets("Rep Data").Range("col").Value = intCol

'newtext = RangeComment.(intCol).Value
'isect.Comment.Text newtext
End If
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to I return the position of a selected cell in a range?

Target is the cell the user selected.

so if you description is one row below

desc = Target.Offset(1,0).Value

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Hi all,

I am trying to create a routine in VBA using which I should be able to
do the following:

- Lets say I have 10 column headers
- They have long descripotions (e.g. "Time spent on.....") but a short
header name (e.g. TM1).
- The descriptions in the corresponding columns in a different row
(hidden)

I was to be able to display a comment with the appropriate text
whenever the user clicks on a header cell.

I am half way there. Code attached: What I can't figure out is how to
return the position of the cell that the user selects so that I can
refer to the right text for the comment. Any help is highly
appreciated.

Code-

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim newtext$
Dim Addr$
Dim RangeToCheck As Range
Dim RangeComment As Range
Dim isect As Range
Dim intRow As Integer
Dim intCol As Integer

If Target.Cells.Count 1 Then
Exit Sub

Set RangeToCheck = ThisWorkbook.Sheets("Rep Data").Range("headers")
RangeComment = ThisWorkbook.Sheets("Rep Data").Range("headertext")
Set isect = Application.Intersect(Target, RangeToCheck)

If isect Is Nothing Then Exit Sub
Addr = isect.Address
'intCol = isect.Row
ThisWorkbook.Sheets("Rep Data").Range("row").Value = Addr
'ThisWorkbook.Sheets("Rep Data").Range("col").Value = intCol

'newtext = RangeComment.(intCol).Value
'isect.Comment.Text newtext
End If
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to I return the position of a selected cell in a range?

Thanks Tom. It's working now. However, I have one small question -
Now, everytime I select a cell in the header row, a comment pops up -
but it remains there when I move to the next one. Is there a way to set
the comments to be visible only when the user has selected that cell?

My revised code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim newtext$
Dim RangeToCheck As Range

If Target.Cells.Count 1 Then Exit Sub

Set RangeToCheck = ThisWorkbook.Sheets("Rep Data").Range("headers")
Set isect = Application.Intersect(Target, RangeToCheck)

If isect Is Nothing Then Exit Sub

newtext = isect.Offset(1, 0).Value

With isect.AddComment
.Visible = True
.Text newtext
End With

End Sub

Thanks again!

Kunal

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to I return the position of a selected cell in a range?

Untested, but possibly:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim newtext$
Dim RangeToCheck As Range

for each cmt in Activesheet.Comments
cmt.Visible = False
Next

If Target.Cells.Count 1 Then Exit Sub

Set RangeToCheck = ThisWorkbook.Sheets("Rep Data").Range("headers")
Set isect = Application.Intersect(Target, RangeToCheck)

If isect Is Nothing Then Exit Sub

newtext = isect.Offset(1, 0).Value

With isect.AddComment
.Visible = True
.Text newtext
End With

End Sub

Although I am not sure why you don't just create all your comments one time
(or in the sheet activate event) and let excel perform it normal behavior of
displaying it when the mouse goes over the cell and hiding if not.

--
Regards,
Tom Ogilvy

wrote in message
oups.com...
Thanks Tom. It's working now. However, I have one small question -
Now, everytime I select a cell in the header row, a comment pops up -
but it remains there when I move to the next one. Is there a way to set
the comments to be visible only when the user has selected that cell?

My revised code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim newtext$
Dim RangeToCheck As Range

If Target.Cells.Count 1 Then Exit Sub

Set RangeToCheck = ThisWorkbook.Sheets("Rep Data").Range("headers")
Set isect = Application.Intersect(Target, RangeToCheck)

If isect Is Nothing Then Exit Sub

newtext = isect.Offset(1, 0).Value

With isect.AddComment
.Visible = True
.Text newtext
End With

End Sub

Thanks again!

Kunal



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to I return the position of a selected cell in a range?

Thanks a ton Tom. That worked! And I agree with you. It was a bit silly
of me not to think of your last suggestion.

Cheers!
Kunal



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Position selected cell nobbyknownowt Setting up and Configuration of Excel 2 February 1st 07 08:00 AM
Formula to return cell position AJPendragon Excel Worksheet Functions 1 February 6th 06 09:34 PM
Default cursor/selected cell position after protecting Stilla Excel Worksheet Functions 0 December 8th 05 02:28 PM
find the first blank cell in a range and return me it's position steve alcock Links and Linking in Excel 2 May 13th 05 09:03 AM
Returning Position Points from the currently selected cell Keys1970 Excel Programming 2 December 30th 04 07:11 PM


All times are GMT +1. The time now is 01:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"