Consulting

Results 1 to 9 of 9

Thread: Solved: One Script for Multiple Worksheets

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: One Script for Multiple Worksheets

    My brother has sent me a worksheet which contains 24 (more or less) worksheets. He's wanting some BeforeDoubleClick" actions to be added for each worksheet. (The same actions would apply to all of the worksheets.) I know how to do this on an individual worksheet, for example:

    [vba]
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    'my code
    End Sub
    [/vba]
    Is there a way to create a single BeforeDoubleClick code in a regular Module and make it apply to all sheets?

  2. #2
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    use Workbook_SheetBeforeDoubleClick in the workbook module.
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    As Brian said, in the VBE's ThisWorkbook object add it:
    [VBA]Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    MsgBox "You doubleclicked in Sheet: " & Sh.Name & vbLf & "Target Range: " & Target.Address
    End Sub
    [/VBA]

  4. #4
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks. I think you guy have got me on the right tract. However, I'm receiving an error so there is still something wrong in my code.

    [VBA]
    Private Sub Workbook_SheetBeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    With Target

    If .Interior.ColorIndex = 1 Then
    .Interior.ColorIndex = xlNone
    Else
    .Interior.ColorIndex = 1
    End If
    If .Font.ColorIndex = 6 Then
    .Font.ColorIndex = 0
    Else
    .Font.ColorIndex = 6
    End If
    If .Font.Bold = True Then
    .Font.Bold = False
    Else
    .Font.Bold = True
    End If
    Cancel = True
    End With
    End Sub


    Private Sub Workbook_SelectionChange(ByVal Target As Range)
    With Cells
    .Interior.ColorIndex = xlNone
    .Font.ColorIndex = 0
    .Font.Bold = False
    End With
    End Sub

    Any thoughts?

    [/VBA]

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Maybe

    [VBA]

    Private Sub Workbook_SelectionChange(ByVal Target As Range)
    With Target
    .Interior.ColorIndex = xlNone
    .Font.ColorIndex = 0
    .Font.Bold = False
    End With
    End Sub

    [/VBA]

    Paul

  6. #6
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    [VBA]Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    [/VBA]

    change the top
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  7. #7
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by Paul_Hossler
    Maybe

    [vba]

    Private Sub Workbook_SelectionChange(ByVal Target As Range)
    With Target
    .Interior.ColorIndex = xlNone
    .Font.ColorIndex = 0
    .Font.Bold = False
    End With
    End Sub

    [/vba]
    Paul
    Thanks. I made that change but still getting the error. The error is actually showing to be in the first subroutine. The message indicates that the procedure declaration does not match the description of the event or procedure.

  8. #8
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by BrianMH
    [vba]Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    [/vba]
    change the top
    Thanks. tried that change anyway but it didn't remove the error.
    Last edited by Opv; 04-28-2011 at 12:02 PM.

  9. #9
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    I'm not sure what I did but the error is gone now. Thanks for all the help.

Posting Permissions

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