vba

jono19872006

Board Regular
Joined
Mar 13, 2012
Messages
184
I have found the code below on the internet and currently use it in one of my sheets, essentially when you click in one of the cells in the range it puts a true value in that cell and then if you click again a false – like a tick box except this can be set up quickly for a long range when required.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
Im not great with code and was wondering if anyone could help me to do any of the following two things, first how would I modify this code so that instead of a true/false value being entered into the cell a string of text of my choice was?<o:p></o:p>
<o:p> </o:p>
And secondly and less importantly the .Offset(0, 1).Select part of the code means that when you click on the cell and the value changes the curses then finishes on the adjacent cell, if I take this out then obviously it stays on the cell I have clicked on, however if I then click on the cell again the value doesn’t change until I click into a different cell and back again, eg if I take out the offset code then I click in C1 which changes to true, I click it again and nothing happens however if I click into D1 then back to C1 it changes to FALSE<o:p></o:p>
<o:p></o:p>
<o:p> </o:p>
Private Sub Worksheet_SelectionChange(ByVal Target As Range)<o:p></o:p>
With Target<o:p></o:p>
If (Not Application.Intersect(.Cells, Range("C1:C10")) Is Nothing) _<o:p></o:p>
And (.Cells.Count = 1) Then<o:p></o:p>
Application.EnableEvents = False<o:p></o:p>
.Value = Not (CStr(Target.Value) = "True")<o:p></o:p>
.Offset(0, 1).Select<o:p></o:p>
Application.EnableEvents = True<o:p></o:p>
End If<o:p></o:p>
End With<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Thanks Jon<o:p></o:p>
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi Jon

i think the following would be better for the True/False activity of your first code (remove your first code completely from the code module and use the below):

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("C1:C10")) Is Nothing Then
    Target.Value = Not CBool(Target.Value)
    Cancel = True
End If
End Sub

This way you just need to double click on the cell to change from True to False and back again.

For the string of your choice point, what would happen if the cell already contained this string? Would you want the cell cleared or what?
 
Upvote 0
FWIW, maybe use the double-click event as Firefly suggested, and give it two possible outcomes.

Rich (BB code):
Option Explicit
    
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    With Target
        If (Not Application.Intersect(.Cells, Range("C1:C10")) Is Nothing) And (.Cells.Count = 1) Then
            Application.EnableEvents = False
            
            Select Case .Value
            Case "String 1"
                .Value = "String 2"
            Case Else
                .Value = "String 1"
            End Select
            
            Application.EnableEvents = True
        End If
    End With
End Sub
 
Upvote 0
thanks instead of the double click what line can i use for a single left hand mouse click?

i have found how to do it with a right click as per Worksheet_beforerightclick but not sure how to do it with a left click?
 
Upvote 0
There isn't one for single left-click (you will have to use Selection_Change in which case you will continue to have the problem of needing to select another cell first if you want to reapply the change to the currently active cell).
 
Upvote 0
If i use the code suggested, how can i update this code that i can use the left mouse button ie Worksheet_selectionchange but incorporate
.Offset(0, 1).Select into the code so that it doesnt remain on the cell afte rthe change


also i have got the code for how i want the text to be edited,

With Target.Font<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
.Name = "Marlett"<o:p></o:p>
'.FontStyle = "Bold" - if want bold in<o:p></o:p>
.Size = 12<o:p></o:p>

at which point can i incorporate this in?

Option Explicit<o:p></o:p>
<o:p></o:p>
Private Sub Worksheet_beforerightclick(ByVal Target As Range, Cancel As Boolean)<o:p></o:p>
Cancel = True<o:p></o:p>
With Target<o:p></o:p>
If (Not Application.Intersect(.Cells, Range("C1:C10")) Is Nothing) And (.Cells.Count = 1) Then<o:p></o:p>
Application.EnableEvents = False<o:p></o:p>
<o:p></o:p>
Select Case .Value<o:p></o:p>
Case "b" 'string 1<o:p></o:p>
.Value = "" 'string 2<o:p></o:p>
Case Else<o:p></o:p>
.Value = "b" 'string 1<o:p></o:p>
End Select<o:p></o:p>
<o:p></o:p>
Application.EnableEvents = True<o:p></o:p>
End If<o:p></o:p>
End With<o:p></o:p>
End Sub<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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