Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I'd like excel to move the number one in column A to a row the user selects with the mouse (or to the active cell is). For instance if user selects C6 the macro would put the numer one in A6 and when user selects another cell in a another row for instance D9 it would "delete" the number one in A6 and move it to A9 and so on...
So far I managed this, but it doesn't get me very long.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Range("c6").Address Then _
Target.Offset(0, -2).Range("a1") = 1End Sub
Can someone please help me,
Regards,
Mark

You're working too hard! ;-)
Putting the 1 in column A of the target row is fairly easy. You don't need to determine what cell was selected. You could use either of these lines:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells(Target.Row, 1) = 1
'or
Range("A" & Target.Row) = 1
End Sub
Deleting the previous 1 might be a little tougher. If there is nothing else in Column A, then you can simply clear the entire column or a specific range before you add the "new" 1. This clears the entire column.
Columns(1).ClearContents
Cells(Target.Row, 1) = 1
However, if you have other data in column A that you need to retain, then you'll need to search for the 1 and delete it.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Columns(1)
Set c = .Find(1, LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then c.Delete
End With
Range("A" & Target.Row) = 1
End Sub
This assumes that it is the only 1 in column A. If you have other 1's in Column A that you don't want deleted, it gets even more complex.

![]() |
Outlook 02, freezes sendi...
|
Excel count function
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |