VBA Range.Offsets must not go beyond set cell range

saesa

New Member
Joined
Jan 17, 2020
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
How can I make perform this macro in given range A1:G5 for example? If macro is supposed to go beyond range with offsets, it has to stay on the last cell within the range.

Thanks.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Which macro are you referring to?
 
Upvote 0
Function RShift()
Selection.Offset(0, 2).Value2 = Selection.Value2
Selection.Value2 = "Moved"
Selection.Offset(0, 2).Select

I want to move cell with right key until i hit the border of selection.
 
Upvote 0
You can't link a macro to the right arrow key, just CTRL-? key combinations.
Can you provide a little bit more about your final goal?
 
Upvote 0
OK, there you go. I did link a macro to just right arrow. When I press right arrow, selected cell moves to right side.


I want the macro to not exceed boundaries of A300:G310 with offsets.
 
Upvote 0
Presumably you're using the Worksheet_SelectionChange Event, and when the cursor moves, the value2 of the previous chosen cell is changed ("Moved"). Am i right?
 
Upvote 0
No, just this:
Application.OnKey "{LEFT}", "LShift"
Application.OnKey "{RIGHT}", "RShift"
Application.OnKey "{UP}", "UShift"
Application.OnKey "{DOWN}", "DShift"
Application.OnKey "{NUMLOCK}", "TabShift"
Application.OnKey "{ENTER}", "EnterShift"


So any ideas? :)
 
Upvote 0
VBA Code:
Dim dColumn as Range
With Selection
    If .Row <=5 And .Column <=6 Then
        Select Case .Column
            Case Is <=5
                dColumn = .Column + 2
            Case Is = 6
                dColumn = 7
        End Select
        .EntireRow.Cells(1, dColumn).Value = .Value
        .Value = "moved"
        .EntireRow.Cells(1, dColumn).Select
    End If
End With
 
Upvote 0
VBA Code:
Dim dColumn as Range
[B]With Selection[/B]
  ...

Thanks, but gives this.
1579372087599.png
 
Upvote 0
VBA Code:
Sub yoursubname()
' code Mike posted
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,712
Members
449,093
Latest member
Mnur

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