Workbook not running macro on close

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
437
Office Version
  1. 365
Platform
  1. Windows
Hi could someone advise me how to get a workbook to run a macro before it closes please.

I have a workbook called 'StaffDetails' it contains a list of staff names and staff grades. In this work book I have a module with the following code
Code:
Sub SortCol()
Dim rng As Range, r As Range
Set rng = Range("A2:B1000")
Set r = Range("A2")
 
 Application.ScreenUpdating = False
 
 rng.Sort key1:=r, order1:=xlAscending, Header:=xlNo, OrderCustom:=1, _
 MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
 
 Application.ScreenUpdating = True
 
 End Sub

And on ThisWorkbook I have the this code
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
Call SortCol
ActiveWorkbook.Save
End Sub

In another workbook book I have a userform with the following code behind the Commandbutton click event

Code:
Private Sub CommandButton1_Click()
Dim StaffName As String
Dim StaffGrade As String
Dim StaffDetails As Workbook
If Textbox1.Text = "" Or ComboBox1.Text = "" Then
        MsgBox "Missing entry.", vbExclamation, "Entry Canceled"
    ElseIf IsNumeric(Application.Match(Textbox1.Text, Sheets("StaffDetailsList").Range("A:A"), 0)) Then
        MsgBox "This name is already in the list, please click cancel and look again.", vbExclamation, "Duplicate Entry"
    Else

Worksheets("StaffDetailsList").Select
StaffName = Textbox1.Value
Worksheets("StaffDetailsList").Select
StaffGrade = ComboBox1.Value
Workbooks.Open ("C:\Users\Paul\Desktop\2017 Diaries\StaffDetails.xlsm")
Worksheets("StaffList").Select
Worksheets("StaffList").Range("A1").Select
RowCount = Worksheets("StaffList").Range("A1").CurrentRegion.Rows.Count
With Worksheets("StaffList").Range("A1")
.Offset(RowCount, 0) = StaffName
.Offset(RowCount, 1) = StaffGrade
End With
Workbooks("StaffDetails").Close SaveChanges:=True
End If
 
Unload Me
MsgBox "Your entry has been accepted. Please return to the date(s) required to enter the name on the  list", vbInformation, "Entry Accepted"
 
End Sub

Everything works fine apart from the columns in the first workbook StaffDetails don't sort themselves when the workbook closes, although if I close it manually then the SortCol macro works fine.

I'm thinking it has something to do with the commandbutton click event but to be honest I'm not that sure and have tried loads of different methods to get the workbook to close and sort the columns.


Please could someone let me know where I'm going wrong

Many thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
First thing you must do is qualify your ranges with a sheet name in the sort macro. Once you have done that report back if further problems.
 
Upvote 0
Hi Steve thanks for the quick reply. My VB knowledge is basic to say the least - I google everything before asking hence all the code above lol.

I have changed it to this - is that what you mean by Qualifying it?

Code:
Sub SortCol()
Dim rng As Range, r As Range
Set rng = Range("A2:B1000")
Set r = Range("A2")
 
With Workbooks("StaffDetails.xlsm").Worksheets("StaffList")
    Application.ScreenUpdating = False
    rng.Sort key1:=r, order1:=xlAscending, Header:=xlNo, OrderCustom:=1, _
    MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
 
    Application.ScreenUpdating = True
End With
 
End Sub
 
Upvote 0
No these two lines need a sheet name:

Set rng = Range("A2:B1000")
Set r = Range("A2")

if no sheet is used the activesheet at the time the line is run is used. That may or may not be the correct sheet to apply the sort to!
 
Upvote 0
Ahh ok I think I understand what you mean so If I changed it to

Set rng = Workbooks ("StaffDetails").Range ("A2:B1000") would that qualify it?

Thanks again

Paul
 
Upvote 0
No. Its the sheet name you need there. The parent of a range is the sheet to which it belongs. The parent of the sheet is the workbook.
 
Upvote 0
Hi Steve

So I have changed it to the following but it is still not sorting the columns once closed.

Code:
Dim rng As Range, r As Range
Set rng = Worksheets("StaffList").Range("A2:B1000")
Set r = Worksheets("StaffList").Range("A2")
 
    Application.ScreenUpdating = False
    rng.Sort key1:=r, order1:=xlAscending, Header:=xlNo, OrderCustom:=1, _
    MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
 
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
See if this does it:

Code:
Dim rng As Range, r As Range, sh As Worksheet, lr As Long
Set sh = Sheets("StaffList")
lr = sh.Range("A" & Rows.Count).End(xlUp).Row
Set rng = sh.Range("A2:B" & lr)
Set r = sh.Range("A" & lr)
sh.Sort.SortFields.Clear
sh.Sort.SortFields.Add Key:=r, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With sh.Sort
    .SetRange rng
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,221
Members
453,152
Latest member
ChrisMd

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