[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

onclick events in excel

Posted on 2000-02-20
10
Medium Priority
?
448 Views
Last Modified: 2008-03-04
is there a way to create an onclick event for a cell. what i want to do is lets say that i have a value in each of three cells a1, a2, and a3 and i want to be able to click on any of those cells and have the value in that cell populate cell c1. if someone could post some code that would be great.
0
Comment
Question by:Bangerter
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:antrat
ID: 2541343
Hi Bangerter

There is no way to do exactly what you want but here are 2 alternatives, the first works by double clicking either A1, A2 or A3 and the 2nd works by right clicking on them.

To put in the code that suits your needs simply right click on the sheet picture with the X on it, top left of toolbar next to "File" and select "View Code" then simply paste the code in.



Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As _ Object, ByVal Target As Excel.Range, Cancel As Boolean)
If Target = Range("A1") _
Or Target = Range("A2") _
Or Target = Range("A3") Then
Range("C1").Value = Target.Value
End If
End Sub





Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As _ Object, ByVal Target As Excel.Range, Cancel As Boolean)
If Target = Range("A1") _
Or Target = Range("A2") _
Or Target = Range("A3") Then
Range("C1").Value = Target.Value
End If
End If
End Sub

antrat


0
 
LVL 9

Expert Comment

by:antrat
ID: 2541360
that should read

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As _
 Excel.Range, Cancel As Boolean)
If Target = Range("A1") _
Or Target = Range("A2") _
Or Target = Range("A3") Then
Range("C1").Value = Target.Value
End If
End If
End Sub

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As _
 Excel.Range, Cancel As Boolean)
If Target = Range("A1") _
Or Target = Range("A2") _
Or Target = Range("A3") Then
Range("C1").Value = Target.Value
End If
End If
End Sub







0
 
LVL 5

Accepted Solution

by:
TigerMan earned 200 total points
ID: 2541507
Bangerter,

Please forgive me if I am wrong, but I would have thought you could attach some code to the
Private Sub Worksheet_SelectionChange event.

Try the following:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
 
 If Target = Range("A1") Or Target = Range("A2") Or _
    Target = Range("A3") Then
        Range("C1").Value = Target.Value
 End If

End Sub

Dave
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 4

Expert Comment

by:Noggy
ID: 2542320
The comments above are all correct but I have encountered a problem with the Worksheet_Change and Worksheet_SelectionChange Events over the last couple of days:
1. If you have a custom (user defined) function in that worksheet and the cell value that you have changed is one that is used as an argument in that custom function, note that the custom function will be evaluated prior to the event code being executed. This is OK on the whole.

2. However, if, when changing the cell value, you don't press <Enter> to accept the new value but click on another cell instead, the custom function will start to be evaluated until it reaches a DoEvents statement or another call to one of your procedures. THEN the Events' code will run.

So, the upshot is that, if you change values of other cells in your Events and you use procedure 2 above, the values in your cells will not get updated. This is because, in effect, your Event code is being run from within the Custom function (which never allows you to change values of cells elsewhere in the workbook).

Sorry if it's a bit of a tangent to your request but I think it is a cautionary tale to tell. I hope I haven't scared you at all. Under most circumstances, this would not be a problem. In fact, until now, it hasn't been a problem for me - it only came to light last week.

FYI The way that I got around it is that the cell values I was changing in my Event code were storage areas for settings that I was using elsewhere in my code (I don't like using Static variables). So what I'm doing now is storing these settings in an INI file.
0
 
LVL 2

Author Comment

by:Bangerter
ID: 2543735
thanks tigerman that is exactly what i needed.
0
 
LVL 2

Author Comment

by:Bangerter
ID: 2551997
tigerman. i am having a problem with the code you gave. at first glance it worked just fine but when i started to develop the application further i ran into some problems.

Worksheet_SelectionChange(ByVal Target As Excel.Range)

according to the documentation the variable 'Target' should contain the cell that was clicked on i.e. "D1" or "A4" or something like that. however what i have fiquered out is that the variable actually contains the value that is in the cell that is clicked on and not the cellname. how do i get the name of the cell  i.e. "D1" or "A4"... to be what the variable 'Target' contains rather than the value of the cell that is clicked.
0
 
LVL 5

Expert Comment

by:TigerMan
ID: 2552240
Bangerter,

No quite sure where you are going with this one.  The following code does both:
a) pastes the value of the selected cell in C1
b) pastes the reference of the selected cell in D1

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

 If Target = Range("A1") Or Target = Range("A2") Or _
    Target = Range("A3") Then
        Range("C1").Value = Target.Value
 End If
 
 Select Case Target
    Case Range("A1")
       MyCell = "A1"
    Case Range("B1")
       MyCell = "B1"
    Case Range("C1")
       MyCell = "C1"
 End Select

 Range("D1").Value = MyCell
 
End Sub

I hope you can solve your problem with that.

Dave
0
 
LVL 2

Author Comment

by:Bangerter
ID: 2552420
here is my code

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
                       

On Error GoTo handler
Select Case Target
    Case Range("c4")
        Range("h4").Value = Target.Value
        Range("c4").Interior.ColorIndex = 6
        Range("d4").Interior.ColorIndex = 2
        Range("e4").Interior.ColorIndex = 2
        Range("f4").Interior.ColorIndex = 2
    Case Range("d4")
        Range("h4").Value = Target.Value
        Range("c4").Interior.ColorIndex = 2
        Range("d4").Interior.ColorIndex = 6
        Range("e4").Interior.ColorIndex = 2
        Range("f4").Interior.ColorIndex = 2
    Case Range("e4")
        Range("h4").Value = Target.Value
        Range("c4").Interior.ColorIndex = 2
        Range("d4").Interior.ColorIndex = 2
        Range("e4").Interior.ColorIndex = 6
        Range("f4").Interior.ColorIndex = 2
    Case Range("f4")
        Range("h4").Value = Target.Value
        Range("c4").Interior.ColorIndex = 2
        Range("d4").Interior.ColorIndex = 2
        Range("e4").Interior.ColorIndex = 2
        Range("f4").Interior.ColorIndex = 6
    Case Range("c8")
        Range("h8").Value = Target.Value
        Range("c8").Interior.ColorIndex = 6
        Range("d8").Interior.ColorIndex = 2
        Range("e8").Interior.ColorIndex = 2
        Range("f8").Interior.ColorIndex = 2
    Case Range("d8")
        Range("h8").Value = Target.Value
        Range("c8").Interior.ColorIndex = 2
        Range("d8").Interior.ColorIndex = 6
        Range("e8").Interior.ColorIndex = 2
        Range("f8").Interior.ColorIndex = 2
    Case Range("e8")
        Range("h8").Value = Target.Value
        Range("c8").Interior.ColorIndex = 2
        Range("d8").Interior.ColorIndex = 2
        Range("e8").Interior.ColorIndex = 6
        Range("f8").Interior.ColorIndex = 2
    Case Range("f8")
        Range("h8").Value = Target.Value
        Range("c8").Interior.ColorIndex = 2
        Range("d8").Interior.ColorIndex = 2
        Range("e8").Interior.ColorIndex = 2
        Range("f8").Interior.ColorIndex = 6
etc.....
end select


lets say that in cell c4 i have a value of $8.00 and in cell c8 i also have a value of $8.00. when i click on c4 it turns yellow (which is colorindex 6) and the other cells on the same row turn white. that is the way it should work for all the rows. however when i click on cell c8 the code treats it as if i clicked on cell c4 and that is because they have the same values in the two cells.
i hope this makes sence. if not i can send you my speadsheet.
0
 
LVL 2

Author Comment

by:Bangerter
ID: 2556702
i got it figured out. i had to use target.row and target.column to pinpoint the cell that was being selected rather than the value in the cell.
0
 
LVL 5

Expert Comment

by:TigerMan
ID: 2557292
Bangerter,

1. Sorry, I haven't checked the thread for a while - so didn't note your new comment this evening.

2. Glad to see it worked out in the end.

Regards

Dave
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

650 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