+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Open workbook and apply code

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Open workbook and apply code

    Hi,

    I have the following working code, but I need to apply it to another closed workbook. What lines of syntax shall I add to it?

    So I guess I need to first of all open that workbook and just do the following or? Or does worksheetfunction apply to another workbook that needs to be opened first?

    In the code below, worksheets "Random RH" is from the other workbook while the "internal analysis" is from the calling workbook

    Private Sub AnalyzeNewWorkbook_Click()
    Dim b18 As Long
    Dim mPeriod As Range
    
    Dim RHAnalyzable As Range
    Set RHAnalyzable = Worksheets("Random RH").Range("AJ:AJ") 
    Set mPeriod = Worksheets("Internal Analysis").Range("J2")
    
    'counting how many analyzable in a given period for Test
    b18 = Application.WorksheetFunction.CountIfs(RHAnalyzable, "Yes", RHShortPeriod, mPeriod)
    Worksheets("Internal Analysis").Range("B18") = b18 
    
    end sub
    Thank you
    Last edited by Lifeseeker; 01-06-2012 at 12:46 PM.

  2. #2
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Open closed workbook and applpy the code? VBA

    I am not sure if this is what you are after.

    Private Sub AnalyzeNewWorkbook_Click()
    Dim b18 As Long
    Dim mPeriod As Range, RHAnalyzable As Range
    Dim wbPath As Workbook
    
      Set wbPath = Workbooks.Open("C:YourFilePathHere")
            Set RHAnalyzable = wbPath.Worksheets("Random RH").Range("AJ:AJ")
            Set mPeriod = Worksheets("Internal Analysis").Range("J2")
            
            'counting how many analyzable in a given period for Test
            b18 = Application.WorksheetFunction.CountIfs(RHAnalyzable, "Yes", RHShortPeriod, mPeriod)
            Worksheets("Internal Analysis").Range("B18") = b18
      wbPath.Close savechanges:=False
    
    End Sub
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Open closed workbook and applpy the code? VBA

    Quote Originally Posted by JapanDave View Post
    I am not sure if this is what you are after.

    Private Sub AnalyzeNewWorkbook_Click()
    Dim b18 As Long
    Dim mPeriod As Range, RHAnalyzable As Range
    Dim wbPath As Workbook
    
      Set wbPath = Workbooks.Open("C:YourFilePathHere")
            Set RHAnalyzable = wbPath.Worksheets("Random RH").Range("AJ:AJ")
            Set mPeriod = Worksheets("Internal Analysis").Range("J2")
            
            'counting how many analyzable in a given period for Test
            b18 = Application.WorksheetFunction.CountIfs(RHAnalyzable, "Yes", RHShortPeriod, mPeriod)
            Worksheets("Internal Analysis").Range("B18") = b18
      wbPath.Close savechanges:=False
    
    End Sub
    Hi JapanDave,

    Yes, that's exactly it!

    If the workbook this code is trying to open is password protected and has macros embedded in it, will this code at least get the workbook to open? Users would still need to type in the password as they normally would to have that workbook fully accessible right by this countifs right?

    Is there any alternative solution without having the need to open that workbook? I thouht I had seen using SUMPRODUCT, but I'm not sure if that will take care of reading data from closed workbook?

  4. #4
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Open closed workbook and applpy the code? VBA

    Try this,

    Private Sub AnalyzeNewWorkbook_Click()
    Dim b18 As Long
    Dim mPeriod As Range, RHAnalyzable As Range
    Dim wbPath As Workbook
    
      Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="YourPassword")
            Set RHAnalyzable = wbPath.Worksheets("Random RH").Range("AJ:AJ")
            Set mPeriod = wbMoto.Worksheets("Internal Analysis").Range("J2")
            
            'counting how many analyzable in a given period for Test
            b18 = Application.WorksheetFunction.CountIfs(RHAnalyzable, "Yes", RHShortPeriod, mPeriod)
            Worksheets("Internal Analysis").Range("B18") = b18
      wbPath.Close savechanges:=False
    
    End Sub
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Open closed workbook and applpy the code? VBA

    Quote Originally Posted by JapanDave View Post
    Try this,

    Private Sub AnalyzeNewWorkbook_Click()
    Dim b18 As Long
    Dim mPeriod As Range, RHAnalyzable As Range
    Dim wbPath As Workbook
    
      Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="YourPassword")
            Set RHAnalyzable = wbPath.Worksheets("Random RH").Range("AJ:AJ")
            Set mPeriod = wbMoto.Worksheets("Internal Analysis").Range("J2")
            
            'counting how many analyzable in a given period for Test
            b18 = Application.WorksheetFunction.CountIfs(RHAnalyzable, "Yes", RHShortPeriod, mPeriod)
            Worksheets("Internal Analysis").Range("B18") = b18
      wbPath.Close savechanges:=False
    
    End Sub
    Hi,

    I got an error saying method not found. The highlighted code is:

      Set wbPath = Workbooks.Open("H:\All Projects\New CCM TIA SPH Macro test 50.xlsm", Password:="SPH")
    I have made sure that the file names are matching. Does this have to do with syntax or?

  6. #6
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Open closed workbook and applpy the code? VBA

    Quote Originally Posted by Lifeseeker View Post
    Hi,

    I got an error saying method not found. The highlighted code is:

      Set wbPath = Workbooks.Open("H:\All Projects\New CCM TIA SPH Macro test 50.xlsm", Password:="SPH")
    I have made sure that the file names are matching. Does this have to do with syntax or?
    Actually I got the filename incorrect....but it's working now thanks

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Open closed workbook and applpy the code? VBA

    Hi Japan,

    The workbook I'm trying to open from this code has a switchboard, and it's been designed in a way that when the workbook is opened, the switchboard will pop up, allowing users to input the password.

    In the code below,
    Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="SPH")
    When I click on the button to open this new workbook from the calling workbook, I find myself STILL having to input the password in order to make worksheets accessible in the newly opened workbook. I thought that password line automatically takes care that?

    for your reference, the code below is associated with the workbook I'm trying to open

    Private Sub Workbook_Open()
         'Turn off screen updates
        Application.ScreenUpdating = False
         
         'Hide confidential sheet at startup
        Call hidesheets
         
         'Activate cell A1 on the Dashboard sheet at startup
        MainPagePHC.Show
        
         'Restore screen updates
        Application.ScreenUpdating = True
    End Sub
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Call hidesheets
    End Sub
    Is this helpful information?

    Thanks

  8. #8
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Open closed workbook and applpy the code? VBA

    Try this,

    Private Sub AnalyzeNewWorkbook_Click()
    Dim b18 As Long
    Dim mPeriod As Range, RHAnalyzable As Range
    Dim wbPath As Workbook
    
    Application.ScreenUpdating = 0
    Application.DisplayAlerts = 0
    
      Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="YourPassword")
            Set RHAnalyzable = wbPath.Worksheets("Random RH").Range("AJ:AJ")
            Set mPeriod = wbMoto.Worksheets("Internal Analysis").Range("J2")
            
            'counting how many analyzable in a given period for Test
            b18 = Application.WorksheetFunction.CountIfs(RHAnalyzable, "Yes", RHShortPeriod, mPeriod)
            Worksheets("Internal Analysis").Range("B18") = b18
      wbPath.Close savechanges:=False
      
    Application.ScreenUpdating = 1
    Application.DisplayAlerts = 1
    
    
    End Sub
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  9. #9
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Open closed workbook and applpy the code? VBA

    Lifeseeker

    Don't quote whole posts -- it's just clutter.

    If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  10. #10
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Open closed workbook and applpy the code? VBA

    Hi Roy,

    It didn't solve the problem. I still had to input the password in order to get into the SPH workbook. Do you have any other alternative solution?

    Thanks

  11. #11
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Open closed workbook and applpy the code? VBA

    My Bad,

    I wrote in the wrong code.

    Private Sub AnalyzeNewWorkbook_Click()
    Dim b18 As Long
    Dim mPeriod As Range, RHAnalyzable As Range
    Dim wbPath As Workbook
    
    Application.ScreenUpdating = 0
    Application.EnableEvents = 0
    
      Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="YourPassword")
            Set RHAnalyzable = wbPath.Worksheets("Random RH").Range("AJ:AJ")
            Set mPeriod = wbMoto.Worksheets("Internal Analysis").Range("J2")
            
            'counting how many analyzable in a given period for Test
            b18 = Application.WorksheetFunction.CountIfs(RHAnalyzable, "Yes", RHShortPeriod, mPeriod)
            Worksheets("Internal Analysis").Range("B18") = b18
      wbPath.Close savechanges:=False
      
    Application.ScreenUpdating = 1
    Application.EnableEvents = 1
    
    
    End Sub
    And my Name is not Roy BTW.
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  12. #12
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Open closed workbook and applpy the code? VBA

    Hi JapanDave,

    I thought I should have made myself a bit more clearer last night as I was thinking more about it, but didn't realize you were so responsive in answering my requests.

    I think the reason that it still asks for password AGAIN in the newly opened workbook is because for the password protection in THAT workbook, I didn't just use the built-in password protection in Excel. Rather, I wrote some self-defined code to first pop up the password window so users would just have to input the password in order to unlock worksheets in THAT workbook.

    I am guessing.....if this version of the code does not work, it probably means that I didn't use the built-in password protection in Excel. Otherwise, the code line
    Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="YourPassword")
    would have granted me access ONCE. Right?

    Let me try your code anyhow and see.

    Thanks

  13. #13
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Open workbook and apply code

    OK, I see now. Can you post a dummy workbook? That would speed things up a lot.
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  14. #14
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Open workbook and apply code

    there you go JapanDave

    I have created dummy workbooks.

    Attached are two workbooks.

    "forjapandaveMain" is the original calling workbook. To get in there, password is test

    once you are in, go to the "analysis" page. On that page, you find a button. click on that button to open another workbook.

    Note: So the password to access the 2nd workbook is "SPH", capitalized.

    Let me know if you cannot get in. (I think you need to go into the VBA window to change the filepath of the 2nd workbook before you can open the 2nd workbook)

    The problem is...because the way the 2nd workbook has been set up, users would have to type in the password AGAIN to unlock worksheets in THAT workbook. So the "password" statement found in your code does not really apply.

    Thanks
    Attached Files Attached Files

  15. #15
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Open workbook and apply code

    I can't believe this, I had written out your code straight into the forum and it crashed.

    Anyway, in the book that is being called, replace the code in the "Workbook" module with the code below. It will allow you to use the password when the worbook is opened by itself and when you are pulling data from it the prompt will not appear. Be careful that the password code will be bypassed if the main workbook is already opened.

    Private Sub Workbook_Open()
        Dim wb As Workbook
          On Error GoTo nxt
         Set wb = Workbooks("YourMainWorbookName") 'The name of the workbook where you are pulling data into
         'Turn off screen updates
        Application.ScreenUpdating = False
         If Not wb Is Nothing Then Exit Sub
                
         'Hide confidential sheet at startup
        'Call hidesheets
    nxt:
         'Activate cell A1 on the Dashboard sheet at startup
        MainPagePHC.Show
        
         'Restore screen updates
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0