Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 34

Thread: Two macro button but in a double action

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location

    Two macro button but in a double action

    Hello
    I am writing to ask for your assistance:
    I did two macro to hide and display rows in my worksheet. I've done two buttons and everything works incredibly well, I turned the internet to find a solution how to get them into a button and perform the same actions. Ie it pressed once to perform (execute) the first macro, it rang a second time to perform the second macro.
    Many ask for your assistance and thank you in advance
    Sub hide()
       Application.ScreenUpdating = False
        Rows("2:2").Select
        Selection.Locked = False
        Selection.FormulaHidden = False
        Range( _
            "4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38,45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79" _
            ).Select
        Range("A79").Activate
            
        Selection.EntireRow.Hidden = True
        Range("A3").Select
        ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
        Application.ScreenUpdating = True
    End Sub
    -----------------------------------------------------------------------------------------
    Sub unhide()
       Application.ScreenUpdating = False
        On Error GoTo ErrHandler
        ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
        Cells.Select
        Selection.EntireRow.Hidden = False
        Range("A1").Select
        Exit Sub
    ErrHandler:
        MsgBox "Wrong password", vbExclamation
        
        'ActiveSheet.Unprotect ("kosta")
        Application.ScreenUpdating = True
    End Sub
    And another thing I do not know how to do - what must complete and how and where it can be implemented both macro I select sheets (for example, the action is for sheet names - "Moscow", "London" , "Italy", "New Zealand", etc.) with just this one button?

  2. #2
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    Sub HideUnhide()    
        Application.ScreenUpdating = False
        If Range("4:4").EntireRow.Hidden = False Then 'Sub hide
            Rows("2:2").Select
            Selection.Locked = False
            Selection.FormulaHidden = False
            Range( _
            "4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38,45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79" _
            ).Select
            Range("A79").Activate
             
            Selection.EntireRow.Hidden = True
            Range("A3").Select
            ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
            Application.ScreenUpdating = True
            
        
        Else 'Sub Unhide
            On Error GoTo ErrHandler
            ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
            Cells.Select
            Selection.EntireRow.Hidden = False
            Range("A1").Select
            Exit Sub
    ErrHandler:
            MsgBox "Wrong password", vbExclamation
             
             'ActiveSheet.Unprotect ("kosta")
            Application.ScreenUpdating = True
        
            
        End If
    
    
    
    
    
    
    
    
    End Sub

    I'm not sure I understand question two.
    You need this macro to function on several sheets when you push the button?

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Have the macro call a listbox where the User can select the location for the remainder of the macro to work with.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Aussiebear with much effort did this macro and I could not finish it, I just do not understand it as you understand it. I do not know where and what must complete must complete to tell the macro - something like a selected sheet names to izplalni macro to hide and then pressing the second show hidden rows.
    If there is anything that is not clear will try to explain more.
    Perhaps what I write is nonsense, but something like:
    Sub HideUnhidee()    
    Application.ScreenUpdating = False
    ' for each sheet with name ("Moscow", "London" , "Italy", "New Zealand", etc.)
        If Range("4:4").EntireRow.Hidden = False Then 'Sub hide
            Rows("2:2").Select
            Selection.Locked = False
            Selection.FormulaHidden = False
            Range( _
            "4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38,45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79" _
            ).Select
            Range("A79").Activate
             
            Selection.EntireRow.Hidden = True
            Range("A3").Select
            ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
            Application.ScreenUpdating = True
             
             
        Else 'Sub Unhide
    ' for each sheet with name ("Moscow", "London" , "Italy", "New Zealand", etc.)
            On Error GoTo ErrHandler
            ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
            Cells.Select
            Selection.EntireRow.Hidden = False
            Range("A1").Select
            Exit Sub
    ErrHandler:
            MsgBox "Wrong password", vbExclamation
             
             'ActiveSheet.Unprotect ("kosta")
            Application.ScreenUpdating = True
             
             
        End If
         
           
         
    End Sub
    You need this macro to function on several sheets when you push the button? - YES

  5. #5
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    I'm typing from my phone now so it's slightly harder to code.

    But look at this:
    http://support.microsoft.com/kb/142126

    Ws_count =..... And down to Next I should be in each of your if statement or else statement.
    And your currect code should be where microsoft page tells your code to be.

    Hope it makes sense

  6. #6
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Hello again,
    if I have to wait as long as necessary.
    Requests, but as far as I understand it makes it replays the entire workbook for all sheets that are in it.
    If we say that this statement is exactly right - it I do not work for me because I filed only an example, and in my workbook I have over 50 sheets and I want to just specific sheets to do so to show or hide.
    I have shown in my previous comment with an example that is certainly wrong, but I do not know how to do it.
    Maybe something like:
    Sheets(Array("Moscow", "London", "Italy", "New Zealand", etc.)).Select
    Many thanks for the support from you, we just have to find the right solution.
    Ie what to do in the code to perform this action for specific sheets.

  7. #7
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    Sub HideUnhide() 
    Dim WS_Count As Integer
    Dim I As Integer    
    Application.ScreenUpdating = False
    If Range("4:4").EntireRow.Hidden = False Then
    'Sub hide 
    WS_Count = ActiveWorkbook.Worksheets.Count
    For I = 1 To WS_Count
    Rows("2:2").Select
    Selection.Locked = False
    Selection.FormulaHidden = Range( "4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34, _
    35:35,38:38,45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79").Select
    Range("A79").Activate
    Selection.EntireRow.Hidden = True
    Range("A3").Select
    ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True, _
    AllowFiltering:=True
    next I 
    Application.ScreenUpdating = True
    Else
    'Sub Unhide
    On Error Goto ErrHandler
    ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
    WS_Count = ActiveWorkbook.Worksheets.Count
     ' Begin the loop.
     For I = 1 To WS_Count
    Cells.Select
    Selection.EntireRow.Hidden = False
    Range("A1").Select
    Next I
    Exit Sub
    ErrHandler: 
    MsgBox "Wrong password", vbExclamation 
    'ActiveSheet.Unprotect ("kosta")
    Application.ScreenUpdating = True
    End If 
    End Sub

    I think this could work. Just a quick fix


    And the forum screwed up my post....

  8. #8
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Hi, I fixed the macro to read, but modifying it gives me an error here.

    Sub HideUnhide12121212() 
    Dim WS_Count As Integer
    Dim I As Integer
    Application.ScreenUpdating = False
    If Range("4:4").EntireRow.Hidden = False Then 'Sub hide
    WS_Count = ActiveWorkbook.Worksheets.Count
    For I = 1 To WS_Count
    Rows("2:2").Select
            Selection.Locked = False '->>> now here gives me error
            Selection.FormulaHidden = False
            Range( _
            "4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38,45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79" _
            ).Select
            Range("A79").Activate
    Selection.EntireRow.Hidden = True
    Range("A3").Select
    ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _  ' and here at least the logic of my macro remains to lock the only the active sheet, and other .....?
    , AllowFiltering:=True
    Next I
    Application.ScreenUpdating = True
    Else 'Sub Unhide
    On Error GoTo ErrHandler
    ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
    WS_Count = ActiveWorkbook.Worksheets.Count
     ' Begin the loop.
    For I = 1 To WS_Count
    Cells.Select
    Selection.EntireRow.Hidden = False
    Range("A1").Select
        Next I
        Exit Sub
    ErrHandler:
        MsgBox "Wrong password", vbExclamation
        'ActiveSheet.Unprotect ("kosta")
        Application.ScreenUpdating = True
        End If
    End Sub
    There is still something not clear to me.
    And it is this - how and where to write just for the sheets to hide, lock and then again just the same to have the opposite effect.
    I will once again give you an example, hoping to be understood:
    I have for example 10 sheets:
    1 - bananas
    2 - london
    3 - strawberries
    4 - Paris
    5 - Italy
    6 - New Zealand
    7 - Japan
    8 - mushrooms
    9 - Greece
    10 - England
    I want the macro action with a button to perform only sheets (London, Paris, Italy, New Zealand, Japan, Greece, England) and does nothing on the other (Bananas, Strawberries, mushrooms)


    In your example, processing the macro - turns out that it absolutely counts all sheets, which leads me to the conclusion that all the sheets(rows) will be hidden, locked, etc.
    Wrong somewhere?
    Thanks for the effort, I think we left a little to finish it.
    Last edited by k0st4din; 09-14-2014 at 06:42 AM.

  9. #9
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    So here's my idea - but this is done by clicking on any one selected from my sheet creating a password and set filters etc.
    Thus I choose himself which are the sheets, but to stop here because it gives me an error - just do not know how to do it,
    Please, for your assistance!!!
    I am convinced that there is a much easier way for things to happen, but I do not know how to do them.
    Please do not mock me
    Sub poslednaproba()
    Application.ScreenUpdating = False
        If Range("4:4").EntireRow.Hidden = False Then 'Sub hide
        Sheets(Array("London", "Italy", "Paris", "UK", "Greece")).Select
        Sheets("London").Activate
        Rows("2:2").Select
        Selection.Locked = False
        Selection.FormulaHidden = False
        Range("4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38,45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79").Select
        Range("A79").Activate
        Selection.EntireRow.Hidden = True
        Range("A2").Select
        Sheets("London").Select
        ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True
        Sheets("Italy").Select
        ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True
        Sheets("Paris").Select
        ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True
        Sheets("UK").Select
        ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True
        Sheets("Greece").Select
        ActiveSheet.Protect ("kosta"), DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True
        Sheets("London").Select
        Range("A2").Select
        Application.ScreenUpdating = True
    Else
        On Error GoTo ErrHandler
        Range("A2").Select
        Sheets("London").Select
        ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
            Cells.Select
            Selection.EntireRow.Hidden = False
            Range("A1").Select
            Exit Sub
    ErrHandler:
            MsgBox "Wrong password", vbExclamation
             
              Application.ScreenUpdating = True
     
        Sheets("Italy").Select
        ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
            Cells.Select
            Selection.EntireRow.Hidden = False
            Range("A1").Select
            Exit Sub
    ErrHandler:
            MsgBox "Wrong password", vbExclamation
        Sheets("Paris").Select
        ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
            Cells.Select
            Selection.EntireRow.Hidden = False
            Range("A1").Select
            Exit Sub
    ErrHandler:
            MsgBox "Wrong password", vbExclamation
        Sheets("UK").Select
        ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
            Cells.Select
            Selection.EntireRow.Hidden = False
            Range("A1").Select
            Exit Sub
    ErrHandler:
            MsgBox "Wrong password", vbExclamation
        Sheets("Greece").Select
        ActiveSheet.Unprotect Password:=InputBox("Please write your password!")
            Cells.Select
            Selection.EntireRow.Hidden = False
            Range("A1").Select
            Exit Sub
    ErrHandler:
            MsgBox "Wrong password", vbExclamation
        Sheets("London").Select
        Range("A2").Select
        End If
    End Sub

  10. #10
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Aussiebear:
    Hello if I can do so if you can otherwise only one button.
    I made a small example
    Attached Files Attached Files

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Option Explicit
    
    Sub poslednaproba()
    Dim RowsToHide As String
    Dim SheetsToUse As Variant
    Dim ThePassWord As String
    Dim TestSheet As String
    Dim i As Long
    
    RowsToHide = "4:5, 10:11, 13:14, 17:18, 34:35, 38:38, 45:46, 49:50, 72:73, 76:79"
    SheetsToUse = Array("London", "Italy", "Paris", "UK", "Greece")
    TestSheet = "London"
        Application.ScreenUpdating = False
        ThePassWord = InputBox("Please write your password!")
        
        'Test Password
        On Error GoTo ErrHandler
        Sheets(TestSheet).Unprotect Password:=ThePassWord
        
        'Check if Rows are hidden
        If Sheets(TestSheet).Range("4:4").EntireRow.Hidden = False Then
          GoTo HideSheets
        Else
          GotoUnHideSheets
        End If
    
    HideSheets:
      For i = LBound(SheetsToUse) To UBound(SheetsToUse)
          With Sheets(SheetsToUse(i))
            .Unprotect Password:=ThePassWord
            .Rows("2:2").Locked = False
            .FormulaHidden = False
            .Range(RowsToHide).EntireRow.Hidden = True
            .Range("A1").Select
            .Protect (ThePassWord), DrawingObjects:=True, Contents:=True, _
                                  Scenarios:=True, AllowFiltering:=True
          End With
      Next i
      GoTo GracefulExit 'Done hiding sheets
    
    UnHideSheets:
      For i = LBound(SheetsToUse) To UBound(SheetsToUse)
          With Sheets(SheetsToUse(i))
            .Unprotect Password:=ThePassWord
            .Range(RowsToHide).EntireRow.Hidden = False
            .FormulaHidden = True
            .Rows("2:2").Locked = True
            .Protect (ThePassWrod), DrawingObjects:=True, Contents:=True, _
                                  Scenarios:=True, AllowFiltering:=True
            .Range("A1").Select
          End With
      Next i
      
    GracefulExit: '
        Application.ScreenUpdating = True
        Exit Sub
        
    ErrHandler:
        MsgBox "Wrong password", vbExclamation
        On Error GoTo 0 'Clear the error
        Application.ScreenUpdating = True
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Hello SamT,
    now try the macro, but first I do not hide anything and the second tells me that the password is wrong.
    in your macro password should be: ThePassWord
    http://prikachi.com/images.php?images/742/7606742N.jpg
    http://prikachi.com/images.php?images/743/7606743e.jpg
    http://prikachi.com/images.php?images/744/7606744n.jpg
    Maybe I wrong somewhere?
    Thank you in advance
    P.S - There's something else I do not see anywhere protect password, ie after hide rows should lock them and then my password wants to display rows and unlock the sheets?
    Last edited by k0st4din; 09-14-2014 at 11:23 AM.

  13. #13
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    "ThePassWord" is a variable. The value is set by the InputBox("Please write your password!")

    In screenshot http://prikachi.com/images.php?images/742/7606742N.jpg use your real password for the workbook.

    Also I make spelling error. Find "ThePassWrod" and change to "ThePassWord"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  14. #14
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Quote Originally Posted by k0st4din View Post
    I did two macro to hide and display rows in my worksheet. I've done two buttons and everything works incredibly well, I turned the internet to find a solution how to get them into a button and perform the same actions. Ie it pressed once to perform (execute) the first macro, it rang a second time to perform the second macro.
    From my understanding of this section, you are seeking a boolean button effect to run the macro. That is, pressed once it caused one effect, and when pressed again, it causes another effect. My interpretation of your code is that you are seeking a method preventing some users, from seeing the formulas used in your workbook.

    And another thing I do not know how to do - what must complete and how and where it can be implemented both macro I select sheets (for example, the action is for sheet names - "Moscow", "London" , "Italy", "New Zealand", etc.) with just this one button?
    Again I'm struggling to understand what you are wanting here, is it that the sheets are known beforehand, or are you wanting to select the sheets on each occasion?

    If you want to protect known sheets (say, "Moscow", "London", "Italy" & "New Zealand", then list them in an array as SamT has kindly shown you, otherwise you need to provide a method for the User to select on the run.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  15. #15
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Quote Originally Posted by Aussiebear View Post
    From my understanding of this section, you are seeking a boolean button effect to run the macro. That is, pressed once it caused one effect, and when pressed again, it causes another effect. My interpretation of your code is that you are seeking a method preventing some users, from seeing the formulas used in your workbook.



    Again I'm struggling to understand what you are wanting here, is it that the sheets are known beforehand, or are you wanting to select the sheets on each occasion?

    If you want to protect known sheets (say, "Moscow", "London", "Italy" & "New Zealand", then list them in an array as SamT has kindly shown you, otherwise you need to provide a method for the User to select on the run.
    Hello Aussiebear
    idea is the following of my excel file:
    Assume that we are in London (UK), in England there are many cities.
    We have 200 people working any products in every city (evenly distributed).
    Several of us, however, work on special concrete products in the same cities that do not work on other people.
    In a workbook we have many worksheets named with names of specific cities + other data.
    Each of these worksheets in column A (in particular rows) are exactly the same only different cities (the names of the sheets).
    At the end of each month, this file is sent to be examined by any person (representative).
    In macros that I have shown and trying to do before I can send files to hide shown me lines (because there are products that should not be visible to others) and to lock all worksheets with names of cities.
    Yes I have and I know the names of the worksheets to be hidden (rows) and locked worksheets.
    In my macro so long (which is good for nothing) - the short version:
    Only one button
    macro to loop through all the worksheets with names of cities (I have to write it down in the macro what those cities) want to hide my rows and lock all worksheets (with city names).
    Second macro - it unlock all the worksheets with the names of cities and unhide the hidden rows.

    SamT
    you write password of a workbook, and I do not have one (excel file is unlocked), I have (ie I want to have passwords worksheets - only selected sheets with names of cities).
    I tried but your macro:
    When I press the button once - as I I go up the word "write your password" then none of the sheets with names of cities not hide any mention of the desired row and lock them.
    This is precisely why I asked if I wrong somewhere?
    In the macro there were two mistakes that spelling is one that you mention, and the other (GotoUnHideSheets) and I fixed it for her (Goto UnHideSheets).
    Please wholeheartedly look at my unworkmanlike macro that I put after hiding rows password worksheets and in your macro I dont see setting a password for worksheets.
    Ie in the example that I gave put (write) password "kosta".
    Thank you all for your support, I believe we can find a solution.
    If you have more questions ask, I will answer, I just do not believe that I can create so many cares one macro.
    Thanks again.

  16. #16
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Hi friends,
    I suspect maybe why you do not reply, but only as a tiny, tiny interjected want to ask you nice to look at whether the other place I answered, as there ask such a question that I answer something, somewhere and I know how I broke into my macro. Once my inquiry is the 10th page and no response, I decided to ask you, and we've just got to finish and almost no response.
    Really ask for your understanding and look (read) that there is a close question, but only refers to a worksheet while on this site asking is another.
    Begging your excuse but I do not think that I have broken the rules in some way.
    I hope to be understood, otherwise I do not see why I should glow red.
    With warmest greetings
    If you still have any doubts, you can look for yourself, I do not have an answer.
    It is nothing to do in this case - wait a while and decided to ask you.

    http://www.excelforum.com/excel-prog...to-unlock.html

  17. #17
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    It's not hard to incorporate a toggle single sheet.

    Sub poslednaproba() 
        Dim RowsToHide As String 
        Dim SheetsToUse As Variant 
        Dim TestSheet As String
        Dim ThePassWord As String 
        Dim Result as long 'Add This declaration
        Dim i As Long 
         
        RowsToHide = "4:5, 10:11, 13:14, 17:18, 34:35, 38:38, 45:46, 49:50, 72:73, 76:79" 
        SheetsToUse = Array("London", "Italy", "Paris", "UK", "Greece") 
        TestSheet = "London" 
    
        Application.ScreenUpdating = False 
    
    'Add the following code
        Result = MsgBox(Click YES to toggle this sheet. Click NO to toggle all sheets. Click CANCEL to cancel action", vbYesNoCancel)
        Select Case Result
            Case vbOK
               SheetsToUse = Array(ActiveSheet.Name)
               TestSheet = SheetsToUse(LBound(SheetsToUse))
           Case vbCancel
               Exit Sub
         End Select
    'End Add Code Section
    
        ThePassWord = InputBox("Please write your password!")
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  18. #18
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Well, you had a password in your macro, so I put one in mine.

    It should work if you just comment out or remove all references to a password.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  19. #19
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    SamT hello
    I do not understand macros from clear to 1000%, in that there is absolutely no question but surely I do not understand or do not see - in your macro is incredibly made​​, but I can not see inside him where to enter the password (see himself that I was mistaken, that ThePassWord a password that it was not so).
    It is true that I did the most stupid way that may exist, but in post number 9 each sheet where I hide rows then I locked him sheets.
    And then when I open the workbook (again by button) unlock sheets and unhide rows show.
    Maybe you missed it, no big trouble.
    Let us alive and healthy.
    Regards



    Sub poslednaproba()
        Application.ScreenUpdating = False 
        If Range("4:4").EntireRow.Hidden = False Then 'Sub hide
            Sheets(Array("London", "Italy", "Paris", "UK", "Greece")).Select 
            Sheets("London").Activate 
            Rows("2:2").Select 
            Selection.Locked = False 
            Selection.FormulaHidden = False 
            Range("4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38,45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79").Select 
            Range("A79").Activate 
            Selection.EntireRow.Hidden = True 
            Range("A2").Select 
            Sheets("London").Select 
            ActiveSheet.Protect ("kosta"), ' Please look at these lines, see how I put a password on the sheets - here we do not do something in your macroDrawingObjects:=True, Contents:=True, Scenarios:=True _ 
            , AllowFiltering:=True 
            Sheets("Italy").Select 
            ActiveSheet.Protect ("kosta"), ' Please look at these lines, see how I put a password on the sheets - here we do not do something in your macroDrawingObjects:=True, Contents:=True, Scenarios:=True _ 
            , AllowSorting:=True, AllowFiltering:=True 
            Sheets("Paris").Select 
            ActiveSheet.Protect ("kosta"), ' Please look at these lines, see how I put a password on the sheets - here we do not do something in your macroDrawingObjects:=True, Contents:=True, Scenarios:=True _ 
            , AllowSorting:=True, AllowFiltering:=True 
            Sheets("UK").Select 
            ActiveSheet.Protect ("kosta") 'Please look at these lines, see how I put a password on the sheets - here we do not do something in your macro, DrawingObjects:=True, Contents:=True, Scenarios:=True _ 
            , AllowSorting:=True, AllowFiltering:=True 
            Sheets("Greece").Select 
            ActiveSheet.Protect ("kosta") 'Please look at these lines, see how I put a password on the sheets - here we do not do something in your macro, DrawingObjects:=True, Contents:=True, Scenarios:=True _ 
            , AllowSorting:=True, AllowFiltering:=True 
            Sheets("London").Select 
            Range("A2").Select 
            Application.ScreenUpdating = True 
        Else 
            On Error Goto ErrHandler 
            Range("A2").Select 
            Sheets("London").Select 
            ActiveSheet.Unprotect Password:=InputBox("Please write your password!") 
            Cells.Select 
            Selection.EntireRow.Hidden = False 
            Range("A1").Select 
            Exit Sub 
    ErrHandler: 
            MsgBox "Wrong password", vbExclamation 
             
            Application.ScreenUpdating = True 
             
            Sheets("Italy").Select 'and here again foolishly trying to unlock and show rows
            ActiveSheet.Unprotect Password:=InputBox("Please write your password!") 
            Cells.Select 
            Selection.EntireRow.Hidden = False 
            Range("A1").Select 
            Exit Sub 
    ErrHandler: 
            MsgBox "Wrong password", vbExclamation 
            Sheets("Paris").Select 'and here again foolishly trying to unlock and show rows
            ActiveSheet.Unprotect Password:=InputBox("Please write your password!") 
            Cells.Select 
            Selection.EntireRow.Hidden = False 
            Range("A1").Select 
            Exit Sub 
    ErrHandler: 
            MsgBox "Wrong password", vbExclamation 
            Sheets("UK").Select 'and here again foolishly trying to unlock and show rows
            ActiveSheet.Unprotect Password:=InputBox("Please write your password!") 
            Cells.Select 
            Selection.EntireRow.Hidden = False 
            Range("A1").Select 
            Exit Sub 
    ErrHandler: 
            MsgBox "Wrong password", vbExclamation 
            Sheets("Greece").Select 'and here again foolishly trying to unlock and show rows
            ActiveSheet.Unprotect Password:=InputBox("Please write your password!") 
            Cells.Select 
            Selection.EntireRow.Hidden = False 
            Range("A1").Select 
            Exit Sub 
    ErrHandler: 
            MsgBox "Wrong password", vbExclamation 
            Sheets("London").Select 
            Range("A2").Select 
        End If 
    End Sub


    Sub poslednaproba() 
        Dim RowsToHide As String 
        Dim SheetsToUse As Variant 
        Dim TestSheet As String 
        Dim ThePassWord As String 
        Dim Result As Long 'Add This declaration
        Dim i As Long 
         
        RowsToHide = "4:5, 10:11, 13:14, 17:18, 34:35, 38:38, 45:46, 49:50, 72:73, 76:79" 
        SheetsToUse = Array("London", "Italy", "Paris", "UK", "Greece") 
        TestSheet = "London" 
         
        Application.ScreenUpdating = False 
         
         'Add the following code ->>> please tell me which code might add here?
        Result = MsgBox(Click YES To toggle this sheet. Click NO To toggle all sheets. Click CANCEL To cancel action", vbYesNoCancel) 
        Select Case Result 
        Case vbOK 
            SheetsToUse = Array(ActiveSheet.Name) 
            TestSheet = SheetsToUse(LBound(SheetsToUse)) 
        Case vbCancel 
            Exit Sub 
        End Select 
         'End Add Code Section ->>>>please tell me which code might add here?
         
        ThePassWord = InputBox("Please write your password!")

    Last edited by k0st4din; 09-15-2014 at 10:11 PM.

  20. #20
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Please attach a dummy workbook, with sheets for the cities you currently have. On the first sheet list those sheets which should be available if you know the password
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •