Consulting

Results 1 to 15 of 15

Thread: Solved: Workbook_BeforeClose

  1. #1
    VBAX Contributor
    Joined
    Feb 2008
    Posts
    193
    Location

    Solved: Workbook_BeforeClose

    Is it possible that the workbook don't ask "saves the changes" but simply stores the changes made bij this macro?


    [VBA]Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If pw1 = True Then
    Sheets("2012").Select
    ActiveSheet.Unprotect "zima"
    Range("i205:OC284").Locked = True
    Range("d205:d284").Locked = True
    Range("A312:OC313").Locked = True
    Range("A205:OC314").FormulaHidden = True
    ActiveSheet.Protect password:="zima", DrawingObjects:=False, Contents:=False, Scenarios:= _
    False, AllowFormattingCells:=False
    Rows("205:314").Hidden = True
    End If

    End Sub
    [/VBA]

    Ger

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    add this line before End Sub

    [VBA]ThisWorkbook.Save[/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Contributor
    Joined
    Feb 2008
    Posts
    193
    Location
    get an error:

    Sub of function not defined

    Ger

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What does it show in the Microsoft Objects list for the project in question in the VBIDE. Maybe Dutch Excel is not ThisWorkbook?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Contributor
    Joined
    Feb 2008
    Posts
    193
    Location
    oeps,

    putted a gap between this and workbook.

    It works fine.

    Thx

    Ger

  6. #6
    VBAX Contributor
    Joined
    Feb 2008
    Posts
    193
    Location
    Next problem. users with pw2 get an error because they open the workbook as read only.
    which code must i use to exit the workbook without saving for users with pw2?

    Ger

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is pw2?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    A total guess. Might pw2 be a Boolean indicating that the user entered an acceptable second level password?

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I thought that, but tried to check.

    At a guess, maybe this does what you want

    [vba]

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If pw1 Then

    With Me

    With .Worksheets("2012")

    .Unprotect "zima"
    .Range("A205:OC314", "D205284", "I205:OC284").FormulaHidden = True
    .Rows("205:314").Hidden = True
    .Protect Password:="zima"
    End With

    .Save
    End With

    ElseIf pw2 Then

    Me.Saved = True
    End If
    End Sub
    [/vba]
    Last edited by Bob Phillips; 12-06-2011 at 04:11 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Contributor
    Joined
    Feb 2008
    Posts
    193
    Location
    There are 2 kind of users. each with his own password (pw1 and pw2). Users with pw1 may change and save. Users with pw2 may only look (read only).

    I think in the code there must be something like if pw2 = true then exit without saving elseif pw2 = true then Thisworkbook.save

    Ger

  11. #11
    VBAX Contributor
    Joined
    Feb 2008
    Posts
    193
    Location
    I changed a few things (the , between the ranges gives an error).
    This works fine.
    What does "with me" do?

    [VBA]Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If pw1 = True Then

    With Me

    With .Worksheets("2012")

    .Unprotect "zima"
    .Range("A205:OC314").FormulaHidden = True
    .Range("D205284").FormulaHidden = True
    Range("I205:OC284").FormulaHidden = True
    .Rows("205:314").Hidden = True
    .Protect password:="zima"
    End With

    .Save
    End With

    ElseIf pw2 = True Then

    Me.Saved = True
    End If
    End Sub
    [/VBA]

    Ger

  12. #12
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    'Me' refers to the object in which the code is running (in this case ThisWorkbook). The With...End With block simply qualifies all the calls within it that are prefixed with a period - i.e. .Worksheets("2012") and .Save in this case - as relating to the ThisWorkbook object.
    Be as you wish to seem

  13. #13
    VBAX Contributor
    Joined
    Feb 2008
    Posts
    193
    Location
    Thx,

    Ger

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Ger
    I changed a few things (the , between the ranges gives an error).
    Odd, they worked for me.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by xld
    Odd, they worked for me.
    The commas would need to be within the inverted commas, otherwise you are passing three arguments to the Range property.
    Be as you wish to seem

Posting Permissions

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