+ Reply to Thread
Results 1 to 6 of 6

VBA Script Applying to other open workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2019
    Location
    Rapid City
    MS-Off Ver
    Standard 2016
    Posts
    13

    VBA Script Applying to other open workbooks

    I am using a login form to customize which sheets to display based on who logs in. It works fine (though I know it's not the cleanest code) but if another workbook is open, I get "Subscript out of range." It appears to me that it is trying to find worksheets in the already open workbook instead of the one that is being logged into.

    Instead of telling users they have to close all open workbooks before opening this one, is there a way to handle this error?

    Code is below:

    Private Sub cmdLogin_Click()
    
    Dim user As String
    Dim password As String
    
    user = Me.txtUserID.Value
    password = Me.txtPassword.Value
    
    If (user = "admin" And password = "mon57701") Or (user = "rcsales" And password = "lifesong") Or (user = "showroom" And password = "update") Then
    
    
    'THIS SECTION IS USED TO CUSTOMIZED LOGIN
    If user = "rcsales" Then
    Unload Me
    Worksheets("LifeSong Report").Visible = False
    Worksheets("RileyStatus2021").Visible = True
    Worksheets("RileyStatus2021").Protect
    Worksheets("RileySort").Visible = False
    Worksheets("RileySales").Visible = True
    Worksheets("RileySales").Protect
    Worksheets("NCOCalc").Visible = False
    Worksheets("COMMRATE").Visible = False
    Worksheets("SFSales").Visible = False
    Worksheets("Targets").Visible = True
    Worksheets("Targets").Protect
    Application.Visible = True
    frmAdminMenu.Show
    
    
    ElseIf user = "admin" Then
    Unload Me
    Worksheets("LifeSong Report").Visible = True
    Worksheets("RileyStatus2021").Visible = True
    Worksheets("RileyStatus2021").Unprotect
    Worksheets("RileySort").Visible = False
    Worksheets("RileySales").Visible = True
    Worksheets("RileySales").Unprotect
    Worksheets("NCOCalc").Visible = True
    Worksheets("COMMRATE").Visible = False
    Worksheets("SFSales").Visible = True
    Worksheets("Targets").Visible = True
    Worksheets("Targets").Unprotect
    Application.Visible = True
    frmAdminMenu.Show
    
    Else
    Unload Me
    Worksheets("LifeSong Report").Visible = False
    Worksheets("RileyStatus2021").Visible = False
    Worksheets("RileyStatus2021").Protect
    Worksheets("RileySort").Visible = False
    Worksheets("RileySales").Visible = True
    Worksheets("RileySales").Unprotect
    Worksheets("NCOCalc").Visible = False
    Worksheets("COMMRATE").Visible = False
    Worksheets("SFSales").Visible = False
    Worksheets("Targets").Visible = False
    Worksheets("Targets").Protect
    Application.Visible = True
    frmAdminMenu.Show
    
    
    End If
    Attached Files Attached Files
    Last edited by trevryan82; 04-01-2021 at 06:46 PM.

  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
    48,186

    Re: VBA Script Applying to other open workbooks

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code] [/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional.)



    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    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
    Registered User
    Join Date
    07-23-2019
    Location
    Rapid City
    MS-Off Ver
    Standard 2016
    Posts
    13

    Re: VBA Script Applying to other open workbooks

    I'm so sorry for not doing that correctly! I will make sure I don't post incorrectly in the future. I have made the change.

  4. #4
    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
    48,186

    Re: VBA Script Applying to other open workbooks

    Thank you

    Try:

    
    Private Sub cmdLogin_Click()
    
    Dim user As String
    Dim password As String
    
    user = Me.txtUserID.Value
    password = Me.txtPassword.Value
    
    If (user = "admin" And password = "mon57701") Or (user = "rcsales" And password = "lifesong") Or (user = "showroom" And password = "update") Then
    
    With ThisWorkbook 
    'THIS SECTION IS USED TO CUSTOMIZED LOGIN
      If user = "rcsales" Then
        Unload Me
        .Worksheets("LifeSong Report").Visible = False
        .Worksheets("RileyStatus2021").Visible = True
        .Worksheets("RileyStatus2021").Protect
        .Worksheets("RileySort").Visible = False
        .Worksheets("RileySales").Visible = True
        .Worksheets("RileySales").Protect
        .Worksheets("NCOCalc").Visible = False
        .Worksheets("COMMRATE").Visible = False
        .Worksheets("SFSales").Visible = False
        .Worksheets("Targets").Visible = True
        .Worksheets("Targets").Protect
        Application.Visible = True
        frmAdminMenu.Show
    
      ElseIf user = "admin" Then
        Unload Me
        .Worksheets("LifeSong Report").Visible = True
        .Worksheets("RileyStatus2021").Visible = True
        .Worksheets("RileyStatus2021").Unprotect
        .Worksheets("RileySort").Visible = False
        .Worksheets("RileySales").Visible = True
        .Worksheets("RileySales").Unprotect
        .Worksheets("NCOCalc").Visible = True
        .Worksheets("COMMRATE").Visible = False
        .Worksheets("SFSales").Visible = True
        .Worksheets("Targets").Visible = True
        .Worksheets("Targets").Unprotect
        Application.Visible = True
        frmAdminMenu.Show
    
      Else
        Unload Me
        .Worksheets("LifeSong Report").Visible = False
        .Worksheets("RileyStatus2021").Visible = False
        .Worksheets("RileyStatus2021").Protect
        .Worksheets("RileySort").Visible = False
        .Worksheets("RileySales").Visible = True
        .Worksheets("RileySales").Unprotect
        .Worksheets("NCOCalc").Visible = False
        .Worksheets("COMMRATE").Visible = False
        .Worksheets("SFSales").Visible = False
        .Worksheets("Targets").Visible = False
        .Worksheets("Targets").Protect
        Application.Visible = True
        frmAdminMenu.Show
      End If
    End With

  5. #5
    Registered User
    Join Date
    07-23-2019
    Location
    Rapid City
    MS-Off Ver
    Standard 2016
    Posts
    13

    Re: VBA Script Applying to other open workbooks

    I hang my head in shame for bothering you with such a simple fix! Thanks so much!

  6. #6
    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
    48,186

    Re: VBA Script Applying to other open workbooks

    You're welcome. Thanks for the rep.

    It only becomes simple when you've seen it before, or something similar ... and you remember what you did last time . No need to be embarrassed.

+ 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] Applying same formula to multiple workbooks
    By hollacost69 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2020, 05:32 PM
  2. Replies: 1
    Last Post: 03-31-2016, 02:06 AM
  3. Applying and saving an auto_open macro to all open workbooks
    By kfryar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2014, 04:20 AM
  4. Replies: 2
    Last Post: 03-29-2013, 06:19 PM
  5. Use of Workbooks.Open in VB Script in Excel 2010 for a web source spread sheet hangs
    By PeterGammage in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-25-2013, 01:26 PM
  6. Applying formula from different workbooks
    By excel5111987 in forum Excel General
    Replies: 0
    Last Post: 09-09-2011, 07:20 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