I have a little problem with the macro below - I use data form to add or delete records. When I add a record, it works, but when I delete a record it doesn't work - the error comes up:
"Error 1004 - Sort Method of Range Class failed"
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
Application.ScreenUpdating = False
SHEETS("STUDS").Visible = True
SHEETS("STUDS").Select
ActiveSheet.ShowDataForm
Columns("A:H").Select
Range("A1:H5").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1").Select
SHEETS("STUDS").Visible = xlVeryHidden
Call sourceSheet.Activate
Can anyone help please ?
This is what came up highlighted as a problem:
Range("A1:G11").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Each time I add or delete a records, the Range changes, but macro code stay the same (A1:G11) - it does not change with Range
I have tried new and modified code but it doesn't work either
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
Application.ScreenUpdating = False
With Worksheets("STUDS")
.Visible = True
.ShowDataForm
Dim LastRow As Long
LastRow = .Cells(.Cells.Rows.Count, "A").End(xlUp).Row 'find last used row in column A
.Range("A1:H" & LastRow).Sort Key1:=Range("A1"), Order1:=xlAscending,
Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
.Range("A1").Select 'only needed if you want to jump to the top
.Visible = xlVeryHidden
End With
Application.ScreenUpdating = True
End Sub
The error msge was:
The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort by Box isn't the same or blank
(the first Sort By box is not the same or blank) Pls help :)