+ Reply to Thread
Results 1 to 5 of 5

VBA to check condition on save and do something if a condition isn't met

Hybrid View

  1. #1
    Registered User
    Join Date
    06-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    54

    VBA to check condition on save and do something if a condition isn't met

    Hi

    I am working on some code to make sure that certain columns are hidden and password protected when a user saves a file and have got this far but got stuck - can anyone help me with the part I'm missing please?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Sheets("Headcount").Select

    if.Columns("C").Hidden.. - this is the line I can't get to work - I want it to determine if Column C is already hidden then just save but if not then the column should be hidden and the sheet should be locked with the password in the section below?

    Columns("C:E").Select
    Selection.EntireColumn.Hidden = True
    Range("A1").Select


    ActiveSheet.Protect Password:="password"
    End Sub

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: VBA to check condition on save and do something if a condition isn't met

    Try this
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        
            If Columns("C").Hidden = True Then
        Exit Sub
        Else
            Columns("C").Hidden = True
            ActiveSheet.Protect Password:="password"
        End If
        
    End Sub
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Registered User
    Join Date
    06-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    54

    Re: VBA to check condition on save and do something if a condition isn't met

    Never mind I've found the solution - thanks for your help!

  4. #4
    Registered User
    Join Date
    06-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    54

    Re: VBA to check condition on save and do something if a condition isn't met

    Thanks gmr4evr1, this works

    Just for completeness I would like when I hit save it to revert back to the 'Summary' tab and select cell A1 - I have edited the formula you provided can you point out what is wrong?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Sheets("Headcount").Select
    If Columns("C").Hidden = True Then
    Exit Sub
    Else
    Columns("C:E").Hidden = True
    ActiveSheet.Protect Password:="password"
    Sheets("Summary").Select
    Range("A1").Select
    End If

    End Sub

  5. #5
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: VBA to check condition on save and do something if a condition isn't met

    I messed up a bit. The 1st If statement says if column C is hidden then Exit Sub......this cause the code to stop running if column c was already hidden. I have changed the code a bit to get it to work correctly.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        
        Application.ScreenUpdating = False
        
        With Sheets("Headcount")
            If Columns("C").Hidden = True Then
        End If
            If Columns("C").Hidden = False Then
                Columns("C:E").Hidden = True
            End If
        End With
        
        Sheets("Summary").Activate
        Range("A1").Activate
        
        Sheets("Headcount").Protect Password:="password"
        Application.ScreenUpdating = True
        
    End Sub

+ 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. Replies: 7
    Last Post: 06-01-2015, 06:05 PM
  2. condition check
    By AMITKUMARVERMA in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-01-2014, 02:03 AM
  3. [SOLVED] condition check formula Help
    By cdllt in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 02-26-2013, 01:23 AM
  4. Need Help to Open File from assigned Folder and Check one condition , Delete and Save.
    By Naveed Raza in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-13-2013, 02:23 PM
  5. Check 2 IF Condition
    By zit1343 in forum Excel General
    Replies: 3
    Last Post: 04-20-2012, 12:46 PM
  6. multi condition format? lookup and date condition valid for.
    By D_Rennie in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-13-2009, 11:37 AM
  7. Check delete condition.
    By pichai in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-29-2007, 01:36 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