+ Reply to Thread
Results 1 to 4 of 4

Problem with ActiveSheet VBA code when it is the open but not in focus

Hybrid View

ThiaJay Problem with ActiveSheet VBA... 09-14-2022, 04:38 PM
TMS Re: Problem with ActiveSheet... 09-14-2022, 06:16 PM
ThiaJay Re: Problem with ActiveSheet... 09-14-2022, 06:35 PM
ThiaJay Re: Problem with ActiveSheet... 09-16-2022, 02:04 PM
  1. #1
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Problem with ActiveSheet VBA code when it is the open but not in focus

    My worksheet contains VBA to control the order of tabbing. It activates when sheet is activesheet and disables when it's not. The problem I encountered today, is that I switched to another application, tried to tab and it was trying to open up the workbook containing the tab controlled worksheet.

    Is there anything I can do about this?

    https://docs.google.com/spreadsheets...f=true&sd=true

    Thanks for your help.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,604

    Re: Problem with ActiveSheet VBA code when it is the open but not in focus

    Please post your code and, ideally, upload the workbook, not a link.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Problem with ActiveSheet VBA code when it is the open but not in focus

    ThisWorkbook:

    Option Explicit
    
    Private Sub Workbook_Open()
        ' Bug in Excel:
        ' The Worksheet_Activate event does not fire for the sheet that is active
        ' when the workbook is opened, so call it explicitely. Make sure that
        ' Worksheet_Activate() is declared as Public.
        ' Ignore ActiveSheets without (Public) Worksheet_Activate()
        On Error Resume Next
        Call ActiveSheet.Worksheet_Activate
        On Error GoTo 0
    'Initialise the array
    '    Call ClipOff
    'Make selection on clipboard
        With Sheet10
    'Insert text into Clipboard
        .Unprotect
        .Shapes("Status").TextFrame.Characters.Text = "Input Mode"
        .Protect
        End With
     Application.EnableEvents = False
    ' Add words to cells in specified worksheets and set permissions
        Dim wks As Worksheet
        For Each wks In Worksheets
            If wks.Name = "Property Numbering" Then
                wks.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
                wks.Range("C14,C8").ClearContents
                wks.Range("B2").Value = "'Property Reference Guide (Click Arrow to Start)"
                wks.Range("C14,C8").Value = "'Choose"
            ElseIf wks.Name = "VO Areas" Then
                wks.Protect UserInterFaceOnly:=True, AllowSorting:=True, AllowFiltering:=True
                wks.Range("C4").ClearContents
                wks.Range("C4").Value = "'Choose"
            Else
                wks.Protect UserInterFaceOnly:=True
            End If
        Next
    Application.EnableEvents = True
    End Sub
    Clipboard Worksheet
    Option Explicit
    
    Public IsClipRunning As Boolean
        
    Sub Worksheet_Activate()
    Application.ScreenUpdating = True
     With ActiveWindow
            .DisplayFormulas = False
            .DisplayHeadings = False
            .DisplayGridlines = False
            .DisplayHorizontalScrollBar = False
            .DisplayVerticalScrollBar = True
        End With
        With Application
            .DisplayFullScreen = True
            .DisplayFormulaBar = False
            .DisplayStatusBar = False
            .CommandBars("Full Screen").Visible = True
            .CommandBars("Worksheet Menu Bar").Enabled = False
            .CommandBars("Standard").Visible = False
            .CommandBars("Formatting").Visible = False
        End With
    Call ClipOff
    'Make selection on clipboard
        With Sheet10
    'Insert text into Clipboard
        .Unprotect
        .Shapes("Status").TextFrame.Characters.Text = "Input Mode"
        .Protect
        End With
    Application.OnKey "{TAB}", "Clipboard.ProcessTab"
    Application.OnKey "+{TAB}", "Clipboard.ProcessBkTab"
    'Cells included in array
        Clipboard.arr = Array("$C$7", "$C$8", "$C$9", "$C$10", "$C$11", "$C$13", "$E$7", "$E$10", "$E$13", "$G$13", "$I$13", "$E$16", "$G$16", "$I$16", "$C$16", "$C$19", "$E$19", "$C$22")
    End Sub
      
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim thisAddress As String
        thisAddress = Split(Target.Address, ":")(0)
        Clipboard.strAddress = Target.Address
        If IsClipRunning Then
            If Len(Trim$(Sheet10.Range(thisAddress).Value & "")) Then
                putToClipboard Sheet10.Range(thisAddress).Value
            End If
        End If
    End Sub
      
    Sub Worksheet_Deactivate()
    '  MsgBox "Disabled Clipboard." 'confirms this routine is running
        Application.OnKey "{TAB}"
        Application.OnKey "+{TAB}"
      End Sub
    Clipboard Module
    Option Explicit
    
    Public arr As Variant
    Public strAddress As String
    
    Public Sub ProcessTab()
    Dim i As Integer
    If Len(strAddress) <> 0 Then
        For i = 0 To UBound(arr)
            If arr(i) = Split(strAddress, ":")(0) Then
                If i = UBound(arr) Then
                    i = 0
                Else
                    i = i + 1
                End If
                Exit For
            End If
        Next
        ActiveSheet.Range(arr(i)).Select
    Else
        strAddress = arr(0)
    End If
    End Sub
    
    Public Sub ProcessBkTab()
    Dim i As Integer
    If Len(strAddress) <> 0 Then
        For i = 0 To UBound(arr)
            If arr(i) = Split(strAddress, ":")(0) Then
                If i = 0 Then
                    i = UBound(arr)
                Else
                    i = i - 1
                End If
                Exit For
            End If
        Next
        ActiveSheet.Range(arr(i)).Select
    Else
        strAddress = arr(0)
    End If
    End Sub
    'Copy Text To Clipboard
    Public Function putToClipboard(ByVal theValue As Variant)
        With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 'Allocate new instance of Microsoft Forms 2.0 DataObject
            .SetText theValue & ""
            .PutInClipboard 'Copy all data in the DataObject to the Clipboard (internally calling OleSetClipboard)
        End With
        Sheet10.Range("$C$4") = theValue
    End Function
    
    Public Sub ClipOn()
    Dim thisAddress As String
    thisAddress = Split(strAddress, ":")(0)
    With Sheet10
        .IsClipRunning = True
        ' unprotect and change the color of the "play" button to red (or you may use any color)
        .Unprotect
        .Shapes("Status").TextFrame.Characters.Text = "Copy Mode"
        .Shapes("Status").TextFrame.Characters.Font.Color = RGB(128, 134, 146)
        .Shapes("Status").Fill.ForeColor.RGB = RGB(242, 242, 242)
        .Shapes("Button 33").TextFrame.Characters.Font.Color = RGB(137, 153, 171)
        .Shapes("Button 34").TextFrame.Characters.Font.Color = vbBlack
        Sheet10.Range("C7,C8,C9,C10,C11,C13,C16,C19,C22,E7:I7,E10:I10,E13,I13,E16,E19:I19,G13,G16,I16").Interior.Color = RGB(242, 242, 242)
        Sheet10.Range("C6:C11,C12:C13,C15:C16,C18:C19,E6:I7,E9:I10,E12:E13,E15:E16,G12:G13,G15:G16,I12:I13,I15:I16,E18:I19,C21:I22").Font.Color = RGB(128, 134, 146)
        .Protect
        If Len(Trim$(.Range(thisAddress).Value & "")) Then
            Call putToClipboard(.Range(thisAddress).Value)
        End If
    End With
    End Sub
    
    Public Sub ClipOff()
    With Sheet10
        ' unprotect to re-instate the color of "play" button to black
        .Unprotect
        .Shapes("Status").TextFrame.Characters.Text = "Input Mode"
        .Shapes("Status").TextFrame.Characters.Font.Color = vbBlack
        .Shapes("Status").Fill.ForeColor.RGB = RGB(146, 208, 80)
        .Shapes("Button 33").TextFrame.Characters.Font.Color = vbBlack
        .Shapes("Button 34").TextFrame.Characters.Font.Color = RGB(146, 208, 80)
        Sheet10.Range("C7:C11,C13,C16,C19,C22:I22,E7:I7,E10:I10,E13,G13,I13,E16,G16,I16,E19:I19").Interior.Color = RGB(146, 208, 80)
        Sheet10.Range("C6:C11,C12:C13,C15:C16,C18:C19,E6:I7,E9:I10,E12:E13,E15:E16,G12:G13,G15:G16,I12:I13,I15:I16,E18:I19,C21:I22").Font.Color = vbBlack
        .IsClipRunning = False
        .Protect
    End With
    End Sub
    
    Public Sub ClipClear()
    Dim Answer As Integer
    Answer = MsgBox("Are you sure you wish to clear the data and reset the form?", vbQuestion + vbYesNo + vbDefaultButton2, "Automatic Clipboard")
    If Answer = vbYes Then
        Call ClipOff
    '    MsgBox "Cleared"
        Sheet10.Range("C7:C11,C13,C16,C19,C22:I22,E7:I7,E10:I10,E13,I13,E16,G16,I16,E19:I19").ClearContents
        Sheet10.Range("C7:C11,C13,C16,C19,C22,E7:I7,E10:I10,E13,I13,E16,E19:I19,G13,G16,I16").Interior.Color = RGB(146, 208, 80)
        Sheet10.Range("C6:C11,C12:C13,C15:C16,C18:C19,E6:I7,E9:I10,E12:E13,E15:E16,G12:G13,G15:G16,I12:I13,I15:I16,E18:I19,C21:I22").Font.Color = vbBlack
        Sheet10.Range("A1").Select
        Sheet10.Range("C7").Select
        Sheet10.Range("$C$4") = Null
    Else
        'Do nothing
    End If
    End Sub
    
    Sub Help_Click()
        Dim Help As Integer
        Help = MsgBox("Software relies heavily on the Windows clipboard." & Chr(13) & Chr(13) & _
        "If you need to duplicate information to multiple accounts/properties, use this tool." & Chr(13) & Chr(13) & _
        "Type the information you need to copy, then within ""Clipboard Controls"" click """ & Chr(62) & """ and then any cell you click on will automatically be copied to the clipboard." & Chr(13) & Chr(13) & _
        "To input text, click ""||"" and when finished, click """ & Chr(62) & """ to continue copying.", _
        vbOKOnly + vbInformation, "About Automatic Clipboard")
            If Help = vbOK Then
        End If
    End Sub
    
    
    ' In the code module named Clipboard:
    'Sub TabOn()
    '  MsgBox "The operator just hit <Tab>."
    '  End Sub
    
    'Sub TabBack()
    '  MsgBox "The operator just hit <Tab Back>."
    'End Sub
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Problem with ActiveSheet VBA code when it is the open but not in focus

    Attached as requested.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] ActiveSheet Unprotect to Userform open, and ActiveSheet Protect to Userform close
    By chrismil in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-15-2020, 06:30 AM
  2. [SOLVED] Set Focus on form at workbook open
    By rob_h in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-17-2017, 12:14 PM
  3. [SOLVED] Worksheet focus problem, how do I set focus?
    By MichiganWilliams in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-19-2014, 03:04 PM
  4. Returning focus to a browser tab that is already open?
    By XmisterIS in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-04-2014, 03:38 AM
  5. [SOLVED] How to get a macro to open file using activesheet path
    By mbroxholme in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-16-2013, 10:31 AM
  6. Replies: 0
    Last Post: 09-23-2005, 07:05 PM
  7. tool tip to open when the cell has the focus
    By GRIFFO in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-19-2005, 04:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1