Macro to move highlighted range up or down a row by using arrows

oblix

Board Regular
Joined
Mar 29, 2017
Messages
183
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Is it possible to highlight a range (say B5 to F6) and use a macro to let user move row up or down by arrows?
If so what would the code look like?

Thank you in advance
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Ok, create a new module in project window and paste this in it:
VBA Code:
Public r As Integer 'Row Position
Public c As Integer 'Column Position
Public h As Integer 'Row Height
Public w As Integer 'Column Width
Sub offsetCellsLeft()
Call offsetClearCells
  c = c - 1
  Call offsetDrawCells
End Sub
Sub offsetCellsRight()
  Call offsetClearCells
  c = c + 1
  Call offsetDrawCells
End Sub
Sub offsetCellsUp()
  Call offsetClearCells
  r = r - 1
  Call offsetDrawCells
End Sub
Sub offsetCellsDown()
  Call offsetClearCells
  r = r + 1
  Call offsetDrawCells
End Sub
Sub offsetClearCells()
For j = c To (c + w) - 1
  For i = r To (r + h) - 1
    Cells(i, j).Interior.ColorIndex = xlNone
  Next
Next
End Sub
Sub offsetDrawCells()
For j = c To (c + w) - 1
  For i = r To (r + h) - 1
    Cells(i, j).Interior.ColorIndex = 6
  Next
Next
End Sub
Now double click to Workbook in project window and paste this:
VBA Code:
Private Sub Workbook_Open()
  r = 5
  h = 2
  c = 2
  w = 5
 
  Call offsetDrawCells
  Application.OnKey "{LEFT}", "offsetCellsLeft"
  Application.OnKey "{RIGHT}", "offsetCellsRight"
  Application.OnKey "{UP}", "offsetCellsUp"
  Application.OnKey "{DOWN}", "offsetCellsDown"

End Sub
Save the workbook and close it. Reopen and allow macros :) Have a nice day!
 
Upvote 0
Thank you for solution
 
Upvote 0
Can this Code be changed to hold in alt and arrow keys to move?
 
Upvote 0
Change this part:
VBA Code:
Application.OnKey "{LEFT}", "offsetCellsLeft"
Application.OnKey "{RIGHT}", "offsetCellsRight"
Application.OnKey "{UP}", "offsetCellsUp"
Application.OnKey "{DOWN}", "offsetCellsDown"
To this:
VBA Code:
Application.OnKey "%{LEFT}", "offsetCellsLeft"
Application.OnKey "%{RIGHT}", "offsetCellsRight"
Application.OnKey "%{UP}", "offsetCellsUp"
Application.OnKey "%{DOWN}", "offsetCellsDown"
 
Upvote 0
Solution
Thank you
Much appreciated
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,064
Members
449,090
Latest member
fragment

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