VBA code throwing up error code 400 or 1004 error

coxash123

New Member
Joined
Aug 16, 2016
Messages
37
Can someone please help. I'm almost certain this code has worked fine in the past. I am a teacher and developed this tool to move students and their data from one class to another......

'Moves a student from one set to another

Code:
Sub MoveSet()

DisableCalculation
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim studentToMove As String
studentToMove = Range("MoveName").Value
Dim setToMove As String
setToMove = Range("MoveNewSet").Value
Set setManager = Worksheets("Set Manager")
'Check user has selected a new set.
If setToMove = "" Then
MsgBox "Choose a set."
GoTo complete
End If

'Check user has selected a student.
If studentToMove = "" Then
MsgBox "Choose a student."
GoTo complete
End If
Set NewSheet = Worksheets(setToMove)
Dim newRow As Integer
Dim currentSet As String
Dim currentSheet As Worksheet
Dim currentRow As Integer
'Determine current set of pupil
For Each rCell In Range("Sets")
 Set Wks = Worksheets(rCell.Value)
    For Each studentNames In Wks.Range("C5:C44")
    
        If studentNames.Value = studentToMove Then
        
        currentSet = rCell.Value
        currentRow = studentNames.Row
 
        GoTo checks
        End If
        
    Next studentNames
Next rCell
checks:
'Check student was found
If currentRow < 1 Then
MsgBox "Student not found."
GoTo complete
End If
'Check current set and new set are different and return if same
If currentSet = setToMove Then
MsgBox "Student is already in that set."
GoTo complete
End If
'Find next blank row in new set
For Each studentNames In NewSheet.Range("C5:C44")
    
    If studentNames.Value = "" Then
        
    newRow = studentNames.Row
    GoTo copyStudent
    End If
        
Next studentNames
    
'No blank rows found
MsgBox "No blank rows found."
GoTo complete
'Copies student data from one row to the next
copyStudent:
'Copy cell values from old set to new set
Set OldRow = Wks.Rows(currentRow)
UnprotectSheet (Wks)
UnprotectSheet (NewSheet)
For Each oldCell In OldRow.Cells
    If Not oldCell.HasFormula Then
   
    'Copy data to new cell
    NewSheet.Cells(newRow, oldCell.Column).Value = oldCell.Value
    'Delete data from old cell
    oldCell.Value = ""
    End If
    'Only loop columns which will have data in
    If oldCell.Column > 150 Then
    
    SortSheet (NewSheet)
    SortSheet (Wks)
    
    Set allSheet = Worksheets("All")
    UnprotectSheet (allSheet)
    allSheet.Activate
    allSheet.Range("$A$4:$EP$244").AutoFilter Field:=2, Criteria1:="<>"
    allSheet.Range("A1:A1").Select
    Wks.Activate
    Wks.Range("A1:A1").Select
    NewSheet.Activate
    NewSheet.Range("A1:A1").Select
    setManager.Activate
    ProtectSheet (Wks)
    ProtectSheet (NewSheet)
    ProtectSheet (allSheet)
    allSheet.Protect
    MsgBox "Move complete."
      
    GoTo complete
    
    End If
         
Next oldCell
complete:
EnableCalculation
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

Im happy to share my document...

Thank you in advance,

Ashley
 
Last edited by a moderator:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Ashley

Where do you get the error?
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top