+ Reply to Thread
Results 1 to 8 of 8

merging two codes into one

Hybrid View

cybrkada merging two codes into one 08-02-2014, 05:20 AM
oeldere Re: merging two codes into one 08-02-2014, 05:39 AM
cybrkada Re: merging two codes into one 08-02-2014, 05:52 AM
oeldere Re: merging two codes into one 08-02-2014, 05:57 AM
cybrkada Re: merging two codes into one 08-02-2014, 06:07 AM
cybrkada Re: merging two codes into one 08-02-2014, 06:04 AM
cybrkada Re: merging two codes into one 08-02-2014, 06:13 AM
oeldere Re: merging two codes into one 08-02-2014, 06:22 AM
  1. #1
    Registered User
    Join Date
    12-04-2013
    Location
    Manila, Philippines
    MS-Off Ver
    Professional Plus 2021
    Posts
    24

    merging two codes into one

    Help, How do I merge these two Workbook_Open() into one?

    Private Sub Workbook_Open()
        ''''''''''''''''''''''''''''''''''''''''''''''''
        ' Unhide the sheets. This Workbook_Open event
        ' will run only if macros are enabled. If macros
        ' are not enabled, this code will not run and
        ' only the introduction sheet will be visible.
        ''''''''''''''''''''''''''''''''''''''''''''''''
        UnHideSheets
    
    End Sub
    Private Sub Workbook_Open()
    
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
         If Left(ws.Name, 2) < Format(Date, "mm") Or (Left(ws.Name, 2) = Format(Date, "mm") And Right(ws.Name, 2) < Format(Date, "dd")) Then
                   ws.Protect Password:="abc"
              Else
                   ws.Unprotect Password:="abc"
                   ws.Cells.Locked = True
              End If
         Next
    End Sub

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: merging two codes into one

    Did you already tried this one?

    Private Sub Workbook_Open()
    
    Dim ws As Worksheet
    
    UnHideSheets
    
    For Each ws In ThisWorkbook.Worksheets
         If Left(ws.Name, 2) < Format(Date, "mm") Or (Left(ws.Name, 2) = Format(Date, "mm") And Right(ws.Name, 2) < Format(Date, "dd")) Then
                   ws.Protect Password:="abc"
              Else
                   ws.Unprotect Password:="abc"
                   ws.Cells.Locked = True
              End If
         Next
    End Sub
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Registered User
    Join Date
    12-04-2013
    Location
    Manila, Philippines
    MS-Off Ver
    Professional Plus 2021
    Posts
    24

    Re: merging two codes into one

    I tried your code, and there's no more debug error. However, it looks like the second code doesn't work.
    The first one unhides all the sheets after macro is turned on, the second event locks cells if sheetname (mmdd format) is earlier than today.

    Please help!

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: merging two codes into one

    However, it looks like the second code doesn't work.
    Did it work on it's own (without the change)?

  5. #5
    Registered User
    Join Date
    12-04-2013
    Location
    Manila, Philippines
    MS-Off Ver
    Professional Plus 2021
    Posts
    24

    Re: merging two codes into one

    Oppss it worked now! I don't know what happened! haha, still your codes

  6. #6
    Registered User
    Join Date
    12-04-2013
    Location
    Manila, Philippines
    MS-Off Ver
    Professional Plus 2021
    Posts
    24

    Re: merging two codes into one

    Yes, they both work on their own before, but i want to merge them! :-(

  7. #7
    Registered User
    Join Date
    12-04-2013
    Location
    Manila, Philippines
    MS-Off Ver
    Professional Plus 2021
    Posts
    24

    Re: merging two codes into one

    Sigh! :-(
    it comes and goes, sometimes it will work, sometimes not, especially when adding new sheets :-(

  8. #8
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: merging two codes into one

    Since you use this statement

    Private Sub Workbook_Open()
    It will excecute when you open the file.

+ 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. Help merging two VBA codes
    By andyaf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-01-2013, 11:27 PM
  2. [SOLVED] Compile error: Constant expression required error when merging two Codes
    By Kezwick in forum Outlook Programming / VBA / Macros
    Replies: 8
    Last Post: 06-26-2013, 09:32 AM
  3. [SOLVED] Merging 2 VBA Codes
    By crusader0011 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-30-2013, 05:36 PM
  4. Merging two vba codes
    By yorilla in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 05:19 AM
  5. Merging Two Codes Into one code
    By LoveCandle in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-27-2005, 12:13 AM

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