Automatically insert a 1 if a cell is selected

griffym

New Member
Joined
Jun 12, 2015
Messages
4
I need to be able to automatically insert a 1 when a cell is selected is there a way of doing this. Cells that are not selected remain blank.

I need to apply this to a particular set of cells only so rest of worksheet acts as normal
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Yes, this can be done with a Worksheet_SelectionChange event procedure. An Event Procedure is VBA that is automatically run upon some event happening (like the selection of a cell).

Here is a helpful link: Worksheet Selection Change Event, Excel VBA

If you want to see if a cell within a particular range is selected, you can use "Intersect", as shown in some of the examples here: Worksheet Change Event, Excel VBA

If you have any issues applying this, please respond back with more details (i.e. what your range is).
 
Upvote 0
Hi I have applied a Worksheet Selection Change Event which will now change any cell to a 1 if selected but I cant figure out how to make it just to my range which is Cells B3:N33
my code thus far is (this was based on getting column 2 to be a 1 when selected I now need to expand it (if i do xx instead of 2 i get the whole sheet but ideally I need just a the cells identified above)
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Columns.Count = 1 And Target.Rows.Count = 1 And Target.Column = 2 Then
Target
.Value = 1
End If
End Sub</code>
 
Upvote 0
Try this:
Code:
Private [COLOR=#00008B][FONT=Consolas]Sub[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Worksheet_SelectionChange[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]([/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]ByVal[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Target [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]As[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Excel[/FONT][/COLOR][COLOR=#000000][FONT=Consolas].[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]Range[/FONT][/COLOR][COLOR=#000000][FONT=Consolas])[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]
[/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]
    If[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Target[/FONT][/COLOR][COLOR=#000000][FONT=Consolas].[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]Count[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]=[/FONT][/COLOR][COLOR=#800000][FONT=Consolas]1 [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]And[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Not (Intersect(Target, Range("B3:N33")) Is Nothing) [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Then
[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]        Target[/FONT][/COLOR][COLOR=#000000][FONT=Consolas].[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]Value[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]=[/FONT][/COLOR][COLOR=#800000][FONT=Consolas]1[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]
[/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]    End[/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]If
[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]
[/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]End [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Sub[/FONT][/COLOR]
 
Upvote 0
Hi That's great works fantastic!! and is easy for me to adjust the range when required!! many thanks. Do you know of a way of converting excel files to work as part of a website or an app on a tablet device??
 
Upvote 0
Do you know of a way of converting excel files to work as part of a website or an app on a tablet device??
I don't. Also, it is best to post entirely new questions in a new thread.

If you already have, and haven't gotten any responses, you can "bump" your post by replying to it. We just recommend that you do not "bump" more than once a day (and wait 24 hours from your initial posting before bumping).
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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