Results 1 to 3 of 3

coding error when trying to chanage code to work on same sheet but different area

Threaded View

jabjab coding error when trying to... 12-07-2011, 12:37 PM
nfuids Re: problems with coding that... 12-07-2011, 12:40 PM
jabjab Re: problems with coding that... 12-07-2011, 12:45 PM
  1. #1
    Registered User
    Join Date
    10-07-2011
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    44

    coding error when trying to chanage code to work on same sheet but different area

    I have had a problem within my spread sheet where it would not allow me to retain data and overwrite it, and I was advised to implement VBA code.
    here is the vba code with annotations
    Option Explicit
    
    
     
    Public Sub Worksheet_Change(ByVal Target As Range)
    
     
    
       ' This Sub is a standard VBA event handler. It is automatically invoked
    
       ' every time the content of any cell in this worksheet changes
    
      
    
       ' We are only interested if the user picks a different type of
    
       ' grade. A named range GradeType was created to name this cell.
    
       ' This allows the worksheet format to change without having to change
    
       ' this code.
    
       If Target.Address = Sheet1.[GradeType].Address Then
    
      
    
          ' So the user doesn't see each invidual worksheet change as it happens
    
          Application.ScreenUpdating = False
    
         
    
          ' Where the current data will be saved to
    
          ' These are in the first row, so the number of columns has
    
          ' to be determined on the fly based on how much data is there
    
          Dim FirstSaveTo As Range
    
          Dim LastSaveTo As Range
    
         
    
          ' Where the previous saved data will be restored from
    
          Dim LastRestoreFrom As Range
    
          Dim FirstRestoreFrom As Range
    
         
    
          ' Use variables to define the relevant spaces in the Save sheet
    
          ' depending on what grade type the user selected
    
          If [GradeType] = "Attainment" Then
    
         
    
             Set FirstSaveTo = Save.[AttainmentStart]
    
             Set LastSaveTo = Save.[AttainmentEnd]
    
            
    
             Set FirstRestoreFrom = Save.[EffortStart]
    
             Set LastRestoreFrom = Save.[EffortEnd]
    
            
    
          Else
    
         
    
            Set FirstRestoreFrom = Save.[AttainmentStart]
    
            Set LastRestoreFrom = Save.[AttainmentEnd]
    
           
    
            Set FirstSaveTo = Save.[EffortStart]
    
            Set LastSaveTo = Save.[EffortEnd]
    
           
    
          End If
    
         
    
          ' Save current data
    
         
    
          ' Clear previously saved data
    
          Save.Range(FirstSaveTo, LastSaveTo).EntireColumn.ClearContents
    
          ' Copy current data
    
          Sheet1.Range(Sheet1.[AssessmentFirst], Cells(Sheet1.UsedRange.Rows.Count, Sheet1.[AssessmentLast].Column)).Copy
    
          ' Paste
    
          FirstSaveTo.PasteSpecial xlPasteValues
    
         
    
          ' Restore saved data
    
         
    
          ' Clear current data
    
          Sheet1.Range(Sheet1.[AssessmentFirst], Cells(Sheet1.UsedRange.Rows.Count, Sheet1.[AssessmentLast].Column)).ClearContents
    
          ' Copy saved data
    
          Save.Range(FirstRestoreFrom, Save.Cells(Save.UsedRange.Rows.Count, LastRestoreFrom.Column)).Copy
    
          ' Paste saved data
    
          Sheet1.[AssessmentFirst].PasteSpecial xlValues
    
         
    
          ' Deselect copy area
    
          Application.CutCopyMode = False
    
          
    
          ' Put user back where he started
    
          [GradeType].Select
    
         
    
          Application.ScreenUpdating = True
    
               
    
       End If

    End Sub
    The code that was created works perfectly however the coding ends where the ‘KS5’ section ends , is it possible to have the same code run through the sheet so that the same effect happens? however it must be dependant on the KS4grades/Effort and KS3grades/Effort toggle options respectively. i have also attached a spreadsheet which also conatins the coding used Attachment 131748
    Last edited by jabjab; 12-07-2011 at 12:54 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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