Need VBA for making a action when clicking on a cell

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
826
Office Version
  1. 365
Platform
  1. Windows
I have a application on a spreadsheet and what I would like is when they click on a few specific cells it will run this speech code I found
Code:
Sub State()
Application.Speech.Speak "Enter the state you live in!"
End Sub
I will be doing this on the whole application
Example: Name,City,State,race etc.....
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Maybe the "selection Change" event?

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

Dim sText As String

Select Case Target.Column

    Case 1 'Change to appropriate column numbers
        sText = "Name"
        
    Case 2
        sText = "City"
        
    Case 3
        sText = "State"
        
    Case 4
        sText = "Race"
        
    Case Else
        Exit Sub
        
End Select

Application.Speech.Speak sText

End Sub
 
Upvote 0
Thanks for the reply Gary, Is there away to have the speech code work only when a specific cell is clicked? Example: A5 would say "State", D10 would say "Race"etc...
also could this only apply to a specific sheet instead of the entire workbook.. Thanks again for your help,your getting me in the right direction.
 
Upvote 0
The variable "Target" that is passed to this event is a reference to the selected cell. You can get just about anything you could possibly want to know about the cell from "Target". including the cell address. Something like the sample below would work though it would get very cumbersome if you had a lot of cells to check. That's why I originally had it checking for entire columns.

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

Dim sText As String

Select Case Target.Address

    Case "$D$10" 'Change to appropriate cell addresses
        sText = "Name"
        
    Case "$A$2"
        sText = "City"
        
    Case "$B$7"
        sText = "State"
        
    Case "$C$8"
        sText = "Race"
        
    Case Else
        sText = "Unknown selection"
        
End Select

Application.Speech.Speak sText

End Sub
 
Upvote 0
Thanks again Gary thats what I needed. Is there away to have check just a specific sheet ?
 
Upvote 0
You could put the code in the "Selection Change" event for the sheet you are interested in or check the name of the sheet in the "Sh" variable that is passed into the workbook level event (as I posted it)

Code:
Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

MsgBox Sh.Name

If Sh.Name = "My Target Sheet" Then

'Do stuff

End If
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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