How can I change this Code from being just Column G to Columns G:M

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

The code below works great, but I'd like to expand its target from Column G to Columns G:M
any ideas the best way to do this?

Thanks

Tony


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
LastR = Sheets("Weekly Data Input").Cells(Rows.Count, "F").End(xlUp).Row
    With Application
        If Target.Address = Range("G" & LastR).Address Then
            .OnKey "{TAB}", Me.CodeName & ".GoBack"
            .OnKey "{ENTER}", Me.CodeName & ".GoBack"
            .OnKey "{Down}", Me.CodeName & ".GoBack"
            .OnKey "~", Me.CodeName & ".GoBack"
        Else
            .OnKey "{TAB}"
            .OnKey "{ENTER}"
            .OnKey "{Down}"
            .OnKey "~"
        End If
    End With
Shapes.Range(Array("Group 3")).Top = ActiveWindow.VisibleRange(2, 3).Top
   
End Sub


Private Sub GoBack()
LastR = Sheets("Weekly Data Input").Cells(Rows.Count, "F").End(xlUp).Row
Range("G" & LastR).Select
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Cancel that - retrying
 
Last edited:
Upvote 0
Try again:

You can use the Intersect

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Application.Intersect(Target, Range("G" & LastR & ":M" & LastR)) Is Nothing Then


        MsgBox Target.Address  'your WITH code here

   End If


Shapes.Range(Array("Group 3")).Top = ActiveWindow.VisibleRange(2, 3).Top

End Sub
 
Last edited:
Upvote 0
HI Daverunt,
This is a good idea but the second part
Code:
[LEFT][COLOR=#333333][FONT=monospace]Private Sub GoBack()
LastR = Sheets("Weekly Data Input").Cells(Rows.Count, "F").End(xlUp).Row
Range("G" & LastR).Select
End Sub[/FONT][/COLOR][/LEFT]

Needs to change from G to whatever column they where in? any ideas????
Tony
 
Upvote 0
Oh.

See if this works for you - otherwise I'm stumped
Create a public variable for the column as below so it is accessible outside of the Sub.
You can probably do the same for LastR

Code:
Public Col As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Application.Intersect(Target, Range("G" & LastR & ":M" & LastR)) Is Nothing Then
Col = Split(ActiveCell(1).Address(1, 0), "$")(0) 'Gets the column letter

WITH Code~~~~
End If

Private Sub GoBack()

LastR = Sheets("Sheet4").Cells(Rows.Count, "F").End(xlUp).Row
Range(Col & LastR).Select

End Sub
 
Last edited:
Upvote 0
Tsk. Running on one cell today.

Use the target address.

Code:
Col = Split(Target.Address(1, 0), "$")(0)
 
Upvote 0
Hi Daverunt,

This is great, I can use this to do what I need thank you.
Tony
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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