#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro

I am trying to create a macro-and I am new at this so bare with me-
where if I am within a cell by simply pushing the up or down arrow the
value within the cell will increase or decrease with each key stroke.
Can this be done.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Macro

If by "within a cell" you mean in Edit Mode, then no - macros are
disabled in Edit mode.

If you want this to happen in one particular cell, this may work for you:

Put this in the worksheet code module:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.OnKey "{UP}"
Application.OnKey "{DOWN}"
With Target
If .Count = 1 Then
If Not Intersect(.Cells, Range("J5")) Is Nothing Then
Application.OnKey "{UP}", "AddOne"
Application.OnKey "{DOWN}", "SubtractOne"
End If
End If
End With
End Sub

(change J5 to suit). Put these in a regular code module:

Public Sub AddOne()
With ActiveCell
If IsNumeric(.Value) Then .Value = .Value + 1
End With
End Sub

Public Sub SubtractOne()
With ActiveCell
If IsNumeric(.Value) Then .Value = .Value - 1
End With
End Sub



In article .com,
wrote:

I am trying to create a macro-and I am new at this so bare with me-
where if I am within a cell by simply pushing the up or down arrow the
value within the cell will increase or decrease with each key stroke.
Can this be done.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default Macro

Run the first macro to add a button to the Worksheet Menu Bar that toggles
the behavior of the arrow keys. This button will be temporary and so will
automatically delete upon closing Excel. After adding the button click it and
then test the arrow keys. Click it again to reset to normal.

Sub AddBtn()
With Application.CommandBars(1)
With .Controls.Add(Temporary:=True)
.OnAction = "TogArrowKeys"
.FaceId = 468
End With
End With
End Sub

Sub TogArrowKeys()
Dim btn As CommandBarButton
With Application
Set btn = .CommandBars.ActionControl
If btn.State = msoButtonUp Then
.OnKey "{UP}", "IncrementCell"
.OnKey "{DOWN}", "DecrementCell"
btn.State = msoButtonDown
Else
.OnKey "{UP}"
.OnKey "{DOWN}"
btn.State = msoButtonUp
End If
End With
End Sub

Sub IncrementCell()
With ActiveCell
If Not IsNumeric(.Value) Then Exit Sub
.Value = .Value + 1
End With
On Error GoTo 0
End Sub

Sub DecrementCell()
With ActiveCell
If Not IsNumeric(.Value) Then Exit Sub
.Value = .Value - 1
End With
End Sub

Regards,
Greg


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 747
Default Macro

You can remove the "On Error GoTo 0" line in the IncrementCell macro. A
leftover when I changed my mind on how to deal with the potential of text in
the cells.

Greg

"Greg Wilson" wrote:

Run the first macro to add a button to the Worksheet Menu Bar that toggles
the behavior of the arrow keys. This button will be temporary and so will
automatically delete upon closing Excel. After adding the button click it and
then test the arrow keys. Click it again to reset to normal.

Sub AddBtn()
With Application.CommandBars(1)
With .Controls.Add(Temporary:=True)
.OnAction = "TogArrowKeys"
.FaceId = 468
End With
End With
End Sub

Sub TogArrowKeys()
Dim btn As CommandBarButton
With Application
Set btn = .CommandBars.ActionControl
If btn.State = msoButtonUp Then
.OnKey "{UP}", "IncrementCell"
.OnKey "{DOWN}", "DecrementCell"
btn.State = msoButtonDown
Else
.OnKey "{UP}"
.OnKey "{DOWN}"
btn.State = msoButtonUp
End If
End With
End Sub

Sub IncrementCell()
With ActiveCell
If Not IsNumeric(.Value) Then Exit Sub
.Value = .Value + 1
End With
On Error GoTo 0
End Sub

Sub DecrementCell()
With ActiveCell
If Not IsNumeric(.Value) Then Exit Sub
.Value = .Value - 1
End With
End Sub

Regards,
Greg


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
Macro recorded... tabs & file names changed, macro hangs Steve Excel Worksheet Functions 3 October 30th 09 11:41 AM
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
how to count/sum by function/macro to get the number of record to do copy/paste in macro tango Excel Programming 1 October 15th 04 01:16 PM
macro to delete entire rows when column A is blank ...a quick macro vikram Excel Programming 4 May 3rd 04 08:45 PM
Start Macro / Stop Macro / Restart Macro Pete[_13_] Excel Programming 2 November 21st 03 05:04 PM


All times are GMT +1. The time now is 03:56 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"