Home » excel » excel – Move a row of data on top when new data changed in a cell

excel – Move a row of data on top when new data changed in a cell

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have searched everywhere but haven’t found solution for my work.

I having a sheet of data which is updated by b PLC to control system in my factory. For example, “workstation1” if is currently working will return a signal as 1, if stop, return 0. So on for workstation2,3,4…

The sheet i’m working on has already displayed the status (0 or 1) of the stations, which like the station name in A column, and the status is in B column.
The problem is, i want when the status of a station changes (1 to 0 or 0 to 1), the whole row of that station move all the way up to the top of the table, so that i can check which station has recently operated. Is this possible to do this?
1 more problem is the data changed is FILLED up automatically by the PLC. I tried some marco which trigger when new data is ‘ENTERED’ by keyboard, it didn’t work.
Thank you in advance and sorry for my bad english.

How to&Answers:

In your VBA Editor on the left side select ThisWorkbook, then on the top in the left dropdown menu select Workbook, then in the right dropdown select SheetChange.
Include this code:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Column = 2 and Target.Row>2 Then 'after a value changes in B column
        Application.EnableEvents = False ' to prevent this change from being seen as another change and causing a loop
        Target.EntireRow.Cut
        sh.Range("A2").EntireRow.Insert Shift:=xlDown 'A2 because I'm assuming you have a header in your first row
        Application.EnableEvents = True 'next sheet changes will cause this to run again
    End If
End Sub