Double Click to enter User ID

monk22

New Member
Joined
Jul 31, 2007
Messages
12
Guys, i have some code that allows my users to enter their user ID on double clicking. The worksheet code below shows how on double clicking into Colum 3 (If Target.Column = 3 Then) then the user ID will appear. Q: How do i get the code to work in Columns 3, 5 and 7 but not those inbetween?! I tried using "or" but it allowed the ID to be entered into all columns! i.e. 3,4,5,6 and 7 which is not what I require. In short I need the ID to appear on double clik into every other column. Thanks. See below for code:



Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Dim user As String
'This refers to the function ReturnUserName, which takes the name from the NT environment

If Target.Column = 3 Then
Target = ReturnUserName
Cancel = True
End If

End Sub



Many Thnaks
Monk22
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try

Code:
 If Target.Column = 3 Or Target.Column = 5 Or Target.Column = 7 Then

Edit:

This is better

Code:
If WorksheetFunction.IsOdd(Target.Column) Then
 
Upvote 0
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 

Dim user As String 
'This refers to the function ReturnUserName, which takes the name from the NT environment 

If Target.Column = 3 Then 
Target = Environ("username")
Cancel = True 
ActiveCell.Offset(0,2).Value = ActiveCell.Value
ActiveCell.Offset(0,4).Value = ActiveCell.Value
End If

End Sub

Try that
 
Upvote 0
Edit:

This is better

Code:
If WorksheetFunction.IsOdd(Target.Column) Then
But then the code would run for columns A, I, K, M, ... , IU as well, which (by my reading at least) is not what the OP wants.
 
Upvote 0
Hi guys, thnak you very much for your help. I used the solution from VoG II because I only had a limited number of column to apply the code to.

New Q: How do i combine this with resticting to every other row? The code currently successfully applies to every other column (or the columns I specify) but the entire column. With regards to setting every other row Ive tried AND Target.Row but this does not seem to work.

Thanks in advance
 
Upvote 0
Try

Code:
If WorksheetFunction.IsOdd(Target.Column) And WorksheetFunction.IsOdd(Target.Row) Then

which will restrict it to odd columns and rows.
 
Upvote 0
This works for me

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If WorksheetFunction.IsOdd(Target.Column) And WorksheetFunction.IsOdd(Target.Row) Then
    Target.Value = Environ("username")
    Cancel = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,533
Members
448,969
Latest member
mirek8991

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