Use of variable address with VBA Intersect

ElvisSteel

Board Regular
Joined
Mar 23, 2009
Messages
122
Hi,

I am using Worksheet_SelectionChange to detect when users move around in my worksheet. I need to ensure they do no leave the current row of data until they have completed it fully. To this end I am using a row counter in the VBA to determine where they are and if they select a cell outside of this row, I want to return them to the required cell.

My code looks like this
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 CurrentRow = Sheets("Tables").Range("CurrentRow").Value
' If cell selected not on current row, then move pointer
 If CurrentRow = 1 Then
  If Intersect(Target, Range("E6:I6")) Is Nothing Then
   Range("E6").Select
  End If
 End If
End Sub

As you can see this is a bit basic and I will have to repeat the "If CurrentRow" test for each of the 10 data rows.
Ideally I would like to be able to have the Range within the Intersect statement vary dynamically according to the CurrentRow value. I have tried using Cells instead of Range but cannot get this to work
If I can get this working I assume I can save the current row/column and return them to the same cell.

Thanks in advance

Steve
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Something like:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   currentrow = Sheets("Tables").Range("CurrentRow").Value
   ' If cell selected not on current row, then move pointer
   If Intersect(Target, Cells(currentrow + 5, "E").Resize(, 5)) Is Nothing Then
      On Error Resume Next
      Application.EnableEvents = False
      Cells(currentrow + 5, "E").Select
      Application.EnableEvents = True
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,589
Messages
6,179,744
Members
452,940
Latest member
rootytrip

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