+ Reply to Thread
Results 1 to 4 of 4

Having trouble making VBA code work for more than 1 worksheet

  1. #1
    Registered User
    Join Date
    04-25-2014
    Location
    Los Angeles
    MS-Off Ver
    Excel 2007
    Posts
    16

    Having trouble making VBA code work for more than 1 worksheet

    I have a workbook with 65 tabs. I found some code that changes a cell to green when double clicked. I pasted it behind one of the worksheets and it works great. However, I don't want to have to paste it to all 65 sheets. I know there is a way to make it work on all the workbook's sheets without copying it behind each one but I can not figure it out. I tried copying it behind "This workbook" to no avail. Perhaps the code is only meant for an individual sheet. I'm using Excel 2007. The code is below.


    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
    ' This subroutine colors a cell red when double-clicked then clears it when double-clicked again.
    ' Some values for .ColorIndex are...
    ' Red = 3, Green = 4, Blue = 5, Yellow = 6, Orange = 45
    ' Google "VBA color palette" for more colors

    ' If the cell is clear
    If Target.Interior.ColorIndex = xlNone Then

    ' Then change the background color to red
    Target.Interior.ColorIndex = 4

    ' Else if the cell background color is red
    ElseIf Target.Interior.ColorIndex = 4 Then

    ' Then clear the background
    Target.Interior.ColorIndex = xlNone

    End If

    ' This is to prevent the cell from being edited when double-clicked
    Cancel = True

    End Sub

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,966

    Re: Having trouble making VBA code work for more than 1 worksheet

    Workbook_SheetBeforeDoubleClick(By ....
    Ben Van Johnson

  3. #3
    Forum Contributor
    Join Date
    01-25-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    434

    Re: Having trouble making VBA code work for more than 1 worksheet

    protonLeah is correct

    so, remove the code from the sheet module, open up the module for ThisWorkbook

    Please Login or Register  to view this content.
    Last edited by ThirtyTwo; 06-06-2014 at 05:50 PM.

  4. #4
    Registered User
    Join Date
    04-25-2014
    Location
    Los Angeles
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Having trouble making VBA code work for more than 1 worksheet

    Thanks that worked great!

+ 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] Can a code work in only ONE worksheet?
    By DoctorMorty in forum Excel Programming / VBA / Macros
    Replies: 28
    Last Post: 04-11-2013, 11:45 AM
  2. Making a worksheet macro work for whole workbook
    By john_london in forum Excel General
    Replies: 2
    Last Post: 01-31-2011, 02:06 PM
  3. making a macro run automatically on start and make code work only on first sheet
    By Alheksi in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-12-2010, 04:18 AM
  4. Making this code work
    By beatrice25 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-24-2006, 11:53 AM
  5. Making VBA code from one WB work on another WB
    By ForestRamsey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2005, 01:45 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