Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default [Beginner's] Adding a letter to a cell by clicking the cell

Could somebody give full code for a macro that would do this:

- By clicking a cell in a range of certain defined cells the cell
would be emptied and then a certain letter, e.g. the letter 'A', would
be defined as the value of the cell if the cell did not already have
the letter as its value;

- By clicking a cell in a range of the certain defined cells the cell
would be emptied if the cell already had the letter as its value.

Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default Adding a letter to a cell by clicking the cell

So if the cell is "A" then delete it, If it does not have "A" then
make it an "A"...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
If Target = "A" Then
Target = ""
Exit Sub
ElseIf Target < "A" Then Target = "A"
End If
End If
End Sub

This goes into the worksheet Module, and only works when you select
the range from A1:A10
http://www.contextures.com/xlvba01.html#Worksheet
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default [Beginner's] Adding a letter to a cell by clicking the cell

Try this:

Dim MyRng As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Define the range the macro should impact.
Set MyRng = ActiveSheet.Range("A1:B10")
'If more than one cell was selected, do nothing.
If Target.Cells.Count 1 Then Exit Sub
'If the selected cell is in MyRng...
If Not Intersect(Target, MyRng) Is Nothing Then
'If the cell's value is A, clear it.
If Target.Value = "A" Then
Target.Clear
'Otherwise, set its value to A.
Else
Target.Value = "A"
End If
End If
'Free the object variable.
Set MyRng = Nothing
End Sub

Right-click on the tab of the sheet where this macro should work, then
select 'View code'. The Visual Basic Editor opens. Paste the code above in
the big empty window. Edit the Set MyRng statement to have the correct range.
Select File Close to return to regular Excel.

If you are new to macros, this link to Jon Peltier's site may be helpful:
http://peltiertech.com/WordPress/200...e-elses-macro/

Hope this helps,

Hutch

"pnumminen" wrote:

Could somebody give full code for a macro that would do this:

- By clicking a cell in a range of certain defined cells the cell
would be emptied and then a certain letter, e.g. the letter 'A', would
be defined as the value of the cell if the cell did not already have
the letter as its value;

- By clicking a cell in a range of the certain defined cells the cell
would be emptied if the cell already had the letter as its value.

Thanks!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Adding a letter to a cell by clicking the cell

Thanks, CurlyDave and Tom Hutchins.

Two problems:

- The letter is inserted/removed also when the cell is selected in a
manner other than clicking.
- You cannot run the action in the macro by clicking the cell another
time without selecting another cell in between.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default Adding a letter to a cell by clicking the cell

On Mar 8, 9:14*am, pnumminen wrote:
Thanks, CurlyDave and Tom Hutchins.

Two problems:

- The letter is inserted/removed also when the cell is selected in a
manner other than clicking.
- You cannot run the action in the macro by clicking the cell another
time without selecting another cell in between.


This is not a problem, it is a fact.....
You asked to change the cell when you clicked on it....

You can use right click event so it will only change when you right
click the cell, not when you select it.

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel
As Boolean)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
Cancel = True
If Target = "A" Then
Target = ""
Exit Sub
ElseIf Target < "A" Then Target = "A"
End If
End If
End Sub

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
Adding an "X" or Check to a cell by clicking on the mouse KAZ Excel Discussion (Misc queries) 5 May 6th 23 11:44 AM
Adding letter at the end of cell Wanna Learn Excel Discussion (Misc queries) 5 April 5th 23 01:04 PM
Adding a letter to the start of each cell Colin Hayes Excel Worksheet Functions 6 October 30th 12 01:29 AM
Adding letter P to Cell chrisjack001 Excel Worksheet Functions 0 January 25th 11 09:00 PM
How to make First letter of the cell in capital letter Irshad Alam Excel Programming 17 September 7th 08 04:14 PM


All times are GMT +1. The time now is 11:07 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"