Results 1 to 5 of 5

Thread: Save Workbook For Each Change Made In A Range

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Save Workbook For Each Change Made In A Range

    Good afternoon all,

    My first post asking for help!
    I am trying to create a spreadsheet, with some VBA code.

    I would like two codes (so i can see which works better in the intended environment)

    1) Once a change is made in Column C - Save the workbook (dont show me any dialogue boxes etc)
    2) Once a cell is changed in Column C from blank to non blank - Save the workbook (dont show me any dialogue boxes etc)

    I have some experience with VBA coding, but by no means a guru lol.

    I then have another sheet that reads from this one. (shared drive locations at work) with coding set up to update/refresh values every minute.

    Kind Regards

    Stalker

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Use this in the code module of the specific sheet where you make changes

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Application.Intersect(Target, Range("C:C")) Is Nothing Then
            Application.DisplayAlerts = False
            ThisWorkbook.Save
            Application.DisplayAlerts = True
        End If
        
    End Sub
    Last edited by Excel Fox; 03-22-2013 at 08:51 PM. Reason: Changed SelectionChange To Change
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0
    Hi Excel Fox,

    I already have one code on that sheet
    Code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cell As Range
    Dim myPassword As String
    myPassword = "UnknowN"
    For Each Cell In ActiveSheet.UsedRange
    On Error Resume Next
    MyCase = UCase(Cell)
    If MyCase = "TARGUS" Or MyCase = "YELLOW" Or MyCase = "RAIL" Or MyCase = "IONS" Or MyCase = "EONS" Or MyCase = "WEBSTORE" Or MyCase = "TV" Or MyCase = "CAPTIAL RENT" Or MyCase = "QUICK" Or MyCase = "LIONS" Or MyCase = "GAME" Or MyCase = "DIRECT" Or MyCase = "NEST" Or MyCase = "HELPER" Or MyCase = "OTTER" Then  ' add more if needed
    Application.EnableEvents = False
    Application.Undo
    Application.EnableEvents = True
    ActiveSheet.Protect Password:=myPassword ' Password you have to change what you want
    MsgBox "Incorrect Entry" & vbCr & vbCr & "Please use the correct sheet", vbCritical
        ThisWorkbook.Close savechanges:=True
    End If
    Next
    End Sub
    And when i add yours underneath i get the error 'Ambigious name' ggrr

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0
    Apologies, just realised the code is added into my existing, not as a new code

    Code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cell As Range
    Dim myPassword As String
    myPassword = "UnknowN"
    For Each Cell In ActiveSheet.UsedRange
    On Error Resume Next
    MyCase = UCase(Cell)
    If MyCase = "TARGUS" Or MyCase = "YELLOW" Or MyCase = "RAIL" Or MyCase = "IONS" Or MyCase = "EONS" Or MyCase = "WEBSTORE" Or MyCase = "TV" Or MyCase = "CAPTIAL RENT" Or MyCase = "QUICK" Or MyCase = "LIONS" Or MyCase = "GAME" Or MyCase = "DIRECT" Or MyCase = "NEST" Or MyCase = "HELPER" Or MyCase = "OTTER" Then  ' add more if needed
    Application.EnableEvents = False
    Application.Undo
    Application.EnableEvents = True
    ActiveSheet.Protect Password:=myPassword ' Password you have to change what you want
    MsgBox "Incorrect Entry" & vbCr & vbCr & "Please use the correct sheet", vbCritical
        ThisWorkbook.Close savechanges:=True
    End If
    Next
        If Not Application.Intersect(Target, Range("C:C")) Is Nothing Then
            Application.DisplayAlerts = False
            ThisWorkbook.Save
            Application.DisplayAlerts = True
        End If
    End Sub
    Last edited by Stalker; 03-22-2013 at 08:35 PM. Reason: PS - Thankyou very much for the code!!

  5. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Well apparently, mine was a mistake. It should actually be a Change event, not a SelectionChange event. You don't want the code to fire when there's a change in the selection, but when you change a value in a selection. I've corrected the code above. Use it, and don't use it within your SelectionChange event.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 4
    Last Post: 07-02-2013, 11:32 AM
  2. Replies: 8
    Last Post: 04-16-2013, 02:04 PM
  3. How To Save Macro To Personal Workbook
    By NITIN SHETTY in forum Excel Help
    Replies: 1
    Last Post: 04-07-2013, 01:07 PM
  4. Change Display Range Based On Change of Dropdown Values
    By rich_cirillo in forum Excel Help
    Replies: 2
    Last Post: 03-29-2013, 04:58 AM
  5. Copy Sheets To New Workbook And Save
    By Prabhu in forum Excel Help
    Replies: 5
    Last Post: 09-06-2011, 09:35 PM

Posting Permissions

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