+ Reply to Thread
Results 1 to 9 of 9

Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

Hybrid View

aLi3nZ Excel 2013 Macro only running... 07-19-2017, 12:44 AM
humdingaling Re: Excel 2013 Macro only... 07-19-2017, 12:53 AM
aLi3nZ Re: Excel 2013 Macro only... 07-19-2017, 12:55 AM
humdingaling Re: Excel 2013 Macro only... 07-19-2017, 01:04 AM
aLi3nZ Re: Excel 2013 Macro only... 07-19-2017, 01:16 AM
jolivanes Re: Excel 2013 Macro only... 07-19-2017, 01:21 AM
aLi3nZ Re: Excel 2013 Macro only... 07-19-2017, 01:23 AM
jolivanes Re: Excel 2013 Macro only... 07-19-2017, 01:30 AM
aLi3nZ Re: Excel 2013 Macro only... 07-19-2017, 01:40 AM
  1. #1
    Registered User
    Join Date
    07-19-2017
    Location
    Auckland
    MS-Off Ver
    Microsoft Office 2013
    Posts
    15

    Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    Hi There,

    I would really appreciate some asssistance with this. This Macro is only running successfully on 1 Excel Sheet within an Excel work book that has around 10 sheets. I am wanting it to only run on the active sheet that I am working in at the time. I do not want it to run on all sheets at the same time.

    Have I dont something wrong with the code or is there something further I can add to specifiy this.


    Private Sub Workbook_Open()
    Activesheet.Unprotect Password:="Yourpasswordhere"
    Dim colorIndex As Integer
    colorIndex = 3
    Dim xRg As Range
    Application.ScreenUpdating = False
    For Each xRg In ActiveSheet.UsedRange.Cells
    Dim color As Long
    color = xRg.Interior.colorIndex
    If (color = colorIndex) Then
    xRg.Locked = True
    End If
    Next xRg
    Application.ScreenUpdating = True
    Activesheet.Protect Password:="Yourpasswordhere"
    End Sub
    Last edited by aLi3nZ; 07-19-2017 at 12:54 AM.

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    i think your using the wrong event

    try it under the workbook_sheetactivate event

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        Dim colorIndex As Integer
        Dim xRg As Range
        Dim color As Long
        
        Sh.Unprotect Password:="Yourpasswordhere"
        
        colorIndex = 3
        
        Application.ScreenUpdating = False
        For Each xRg In Sh.UsedRange.Cells
        
            color = xRg.Interior.colorIndex
            If (color = colorIndex) Then
                xRg.Locked = True
            End If
        Next xRg
        
        Application.ScreenUpdating = True
        Sh.Protect Password:="Yourpasswordhere"
    
    End Sub
    Last edited by humdingaling; 07-19-2017 at 01:03 AM. Reason: OP complied with request
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    07-19-2017
    Location
    Auckland
    MS-Off Ver
    Microsoft Office 2013
    Posts
    15

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    Thank you, I have corrected this.

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    thank you...please check post #2
    Last edited by humdingaling; 07-19-2017 at 01:33 AM.

  5. #5
    Registered User
    Join Date
    07-19-2017
    Location
    Auckland
    MS-Off Ver
    Microsoft Office 2013
    Posts
    15

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    Thank you so much for your prompt help humdingaling.

    I will try this out and let you know how it goes.

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    Whichever sheet was the active sheet when last saved will be the ActiveSheet when opening your workbook again.
    If you were working on sheet5, saved the workbook and closed it, when opening the workbook it will open with sheet5 as active sheet.
    It does not sound like you want the code to run when opening the workbook but when you are changing cells.

    Or

    Do you want to run the code just once with a button?
    Last edited by jolivanes; 07-19-2017 at 01:24 AM.

  7. #7
    Registered User
    Join Date
    07-19-2017
    Location
    Auckland
    MS-Off Ver
    Microsoft Office 2013
    Posts
    15

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    Hi Jolivanes,

    Yes, ok, that makes sense. Yes, its true, I want it to run on any of the cells of the sheets that are marked as red in color.

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    Post #2 was updated. Is that what you have in mind?

    Or this?

    Sub Lock_Red_Cells()
    
    Dim lc As Long, lr As Long, c As Range
    
    lc = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    lr = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    
    For Each c In ActiveSheet.Range(Cells(1, 1), Cells(lr, lc))
    
        If c.Interior.Color = vbRed Then c.Locked = True
    
    Next c
    
    End Sub
    Last edited by jolivanes; 07-19-2017 at 01:38 AM. Reason: Add code

  9. #9
    Registered User
    Join Date
    07-19-2017
    Location
    Auckland
    MS-Off Ver
    Microsoft Office 2013
    Posts
    15

    Re: Excel 2013 Macro only running on 1 Sheet within multi sheet Excel Workbook

    I have just tested code in post #2 and it is working perfectly. Thanks so much. I have marked thread as solved. Thanks again guys.
    Last edited by aLi3nZ; 07-19-2017 at 01:53 AM.

+ 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. Replies: 2
    Last Post: 05-13-2016, 01:01 PM
  2. [SOLVED] When running a Macro, it crashes my Excel 2013... Why?
    By fleyd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-24-2015, 11:50 AM
  3. [SOLVED] Excel 2013 - Macro to import sheet from another wb not working.
    By beenbee in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-07-2015, 05:04 AM
  4. Copy rows from one sheet to another sheet excel 2013
    By billisnice in forum Excel General
    Replies: 2
    Last Post: 04-30-2015, 09:41 PM
  5. How To Open an Macro From 2003 Excel in 2013 Excel Spread Sheet?
    By ADubin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-24-2015, 09:22 PM
  6. Excel 2013 - Macro to update new sheet
    By quabba in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-23-2014, 11:53 AM
  7. Automatically transfer data from one sheet to another in a workbook - Excel 2013
    By WGBarry in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2013, 01:03 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