VBA Code - Effecting entire Workbook not just a Worksheet

MDBHR

New Member
Joined
Sep 17, 2018
Messages
10
Office Version
  1. 365
Platform
  1. Windows
So yeah, I'm incredibly new to the code world. I found some VBA Code on this sit for changing the tab order and it worked great! Except that tab order started working on all 6 of the worksheets instead of just the one I had intended. Which is annoying. I think it should be a simple fix. Just cannot figure it out. Any help or suggestions would be greatly appreciated! Here is the code that I currently have running:

In the ThisWorkBook Module...

Code:
  Private Sub Workbook_WindowActivate(ByVal Wn As Window)
  If ActiveSheet.Name = "Customer Information" Then SetOnkey True
  End Sub
 


  Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
  SetOnkey False
  End Sub




In the Sheet Module "Customer Information"...

Code:
  Private Sub Workbook_WindowActivate(ByVal Wn As Window)
  If ActiveSheet.Name = "Customer Information" Then SetOnkey True
  End Sub


 
  Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
  SetOnkey False
  End Sub

In a standard code module...

Code:
Sub SetOnkey(ByVal state As Boolean)
    If state Then
        With Application
            .OnKey "{TAB}", "'TabRange xlNext'"
            .OnKey "~", "'TabRange xlNext'"
            .OnKey "{RIGHT}", "'TabRange xlNext'"
            .OnKey "{LEFT}", "'TabRange xlPrevious'"
            .OnKey "{DOWN}", "do_nothing"
            .OnKey "{UP}", "do_nothing"
        End With
    Else
    'reset keys
        With Application
            .OnKey "{TAB}"
            .OnKey "~"
            .OnKey "{RIGHT}"
            .OnKey "{LEFT}"
            .OnKey "{DOWN}"
            .OnKey "{UP}"
        End With
    End If
End Sub




Sub do_nothing()
'nothing to do
End Sub




Sub TabRange(Optional iDirection As Integer = xlNext)


Dim vTabOrder As Variant, m As Variant
Dim lItems As Long, iAdjust As Long


'--set the tab order of input cells - change ranges as required
vTabOrder = Array("B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "C3", "C4", "C5", "C6", "C7")
lItems = UBound(vTabOrder) - LBound(vTabOrder) + 1


On Error Resume Next
m = Application.Match(ActiveCell.Address(0, 0), vTabOrder, False)
On Error GoTo ExitSub


'--if activecell is not in Tab Order return to the first cell
If IsError(m) Then
   m = 1
Else
   '--get adjustment to index
   iAdjust = IIf(iDirection = xlPrevious, -1, 1)


   '--calculate new index wrapping around list
   m = (m + lItems + iAdjust - 1) Mod lItems + 1
End If


'--select cell adjusting for Option Base 0 or 1
Application.EnableEvents = False
Range(vTabOrder(m + (LBound(vTabOrder) = 0))).Select


ExitSub:
   Application.EnableEvents = True
End Sub
 
@dmt32 Do I at least have the correct code in the correct locations? I will trouble-shoot the SharePoint challenge. I just need to narrow down where I am.

Thank you for your help in advance.

Yes what you have done is correct however, a quick glance at TabOrderArray Function, the quote marks used do not look correct

try this update

VBA Code:
Function TabOrderArray() As Variant
       TabOrderArray = Array("D6", "D12", "F12", "J6", "M6", "J11", "M11", "M14", "D22", "F22", "J21", "J24")
End Function

If still have issues, suggest trying to run code from a local drive

Dave
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Thank you so much @dmt32 ! That worked. I now need to figure out how to address the SharePoint issue. I know that's not your specialty and I really appreciate your help in getting me this far :)
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

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