Sheet to automatically update

danjuma

Active Member
Joined
Sep 21, 2008
Messages
251
Hello all. Below is a VBA macro code I recorded (manually sorting data in range A2:F29 based on the largest to smallest in range F2:F29). I would be most grateful if someone could amend it so that this is done automatically by excel (sort out the data) every say 5 secs. Many thanks

Code:
Sub AutoSort()'
' AutoSort Macro
'
' Keyboard Shortcut: Ctrl+p
'
    Range("A2:F29").Select
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add2 Key:=Range("F2:F29") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("A2:F29")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("G3").Select
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Run the code Start_each.
If you want to stop the execution, execute the code Stop_each

Try this

Code:
Dim vNow  '<---- At the beginning of the entire code


Sub [COLOR=#0000ff]AutoSort[/COLOR]() '
  ' AutoSort Macro
  ' Keyboard Shortcut: Ctrl+p
  '
  With ActiveWorkbook.Worksheets("Sheet2").Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("F2:F29"), SortOn:=xlSortOnValues, _
      Order:=xlDescending, DataOption:=xlSortNormal
    .SetRange Range("A2:F29")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
End Sub


Sub [COLOR=#008000]Start_each[/COLOR]()
  Call AutoSort
  vNow = Now + TimeValue("00:00:05")
  Application.OnTime vNow, "Start_each"
End Sub


Sub [COLOR=#ff0000]Stop_each[/COLOR]()
  On Error Resume Next
  Application.OnTime vNow, "Start_each", , False
End Sub
 
Upvote 0
Run the code Start_each.
If you want to stop the execution, execute the code Stop_each

Try this

Code:
Dim vNow  '<---- At the beginning of the entire code


Sub [COLOR=#0000ff]AutoSort[/COLOR]() '
  ' AutoSort Macro
  ' Keyboard Shortcut: Ctrl+p
  '
  With ActiveWorkbook.Worksheets("Sheet2").Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("F2:F29"), SortOn:=xlSortOnValues, _
      Order:=xlDescending, DataOption:=xlSortNormal
    .SetRange Range("A2:F29")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
End Sub


Sub [COLOR=#008000]Start_each[/COLOR]()
  Call AutoSort
  vNow = Now + TimeValue("00:00:05")
  Application.OnTime vNow, "Start_each"
End Sub


Sub [COLOR=#ff0000]Stop_each[/COLOR]()
  On Error Resume Next
  Application.OnTime vNow, "Start_each", , False
End Sub

Works fine. Many thanks sir. Much obliged.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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