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
Thank youPrivate 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
Last edited by Lifeseeker; 01-06-2012 at 12:46 PM.
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
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?
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
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,
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?Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="SPH")
for your reference, the code below is associated with the workbook I'm trying to open
Is this helpful information?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
Thanks
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
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)
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
My Bad,
I wrote in the wrong code.
And my Name is not Roy BTW.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![]()
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
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
would have granted me access ONCE. Right?Set wbPath = Workbooks.Open("C:\YourFilePath", Password:="YourPassword")
Let me try your code anyhow and see.
Thanks
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
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
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
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks