Hey guys,
I have found some different VBA codes and pieced it together to my current status.
I do have some problems. The row is moving based on the value of column G where "GODKENDT" will move the row. The problem is that not all rows work and sometimes I can close the file, reopen and then the code works. More often or not it works on row numbers below 15-ish but not over. But usually it bugs me so it doesn't work at all.
My code is the following:
I really cant figure out what seems to be the problem to be honest. I have had small moments where it worked like a charm but even a look at the code (not touching it) can bug it.
Working on Office 365.
Thanks in advance.
I have found some different VBA codes and pieced it together to my current status.
I do have some problems. The row is moving based on the value of column G where "GODKENDT" will move the row. The problem is that not all rows work and sometimes I can close the file, reopen and then the code works. More often or not it works on row numbers below 15-ish but not over. But usually it bugs me so it doesn't work at all.
My code is the following:
Code:
[COLOR=#000000][FONT=Verdana]Private Sub Worksheet_Change(ByVal Target As Range)[/FONT][/COLOR][COLOR=#000000][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Dim KeyCells As Range[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Dim lastRow As Integer, lastRowOut As Integer, pasteRow As Integer[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Dim wsÅbne As Worksheet, wsLøst As Worksheet[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Set wsÅbne = ThisWorkbook.Sheets("Aktuelt")[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Set wsLøst = ThisWorkbook.Sheets("Godkendt")[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] lastRow = wsÅbne.Cells(wsÅbne.Rows.Count, 2).End(xlUp).Row[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] ' The variable KeyCells contains the cells that will[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] ' cause an alert when they are changed.[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Set KeyCells = wsÅbne.Range("G2:G" & lastRow)[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] For i = 2 To lastRow[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] lastRowOut = wsLøst.Cells(wsLøst.Rows.Count, 2).End(xlUp).Row[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] If lastRowOut = 1 Then[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] pasteRow = 2[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Else[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] pasteRow = wsLøst.Cells(wsLøst.Rows.Count, 2).End(xlUp).Row + 1[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] End If[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] If wsÅbne.Range("G" & i).Value = "GODKENDT" Then[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] wsÅbne.Range("A" & i & ":G" & i).Cut Destination:=wsLøst.Range("A" & pasteRow)[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] wsÅbne.Rows(i).EntireRow.Delete[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] End If[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]
[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] Next i[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] End If[/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana] [/FONT][/COLOR]
[COLOR=#000000][FONT=Verdana]End Sub[/FONT][/COLOR]
I really cant figure out what seems to be the problem to be honest. I have had small moments where it worked like a charm but even a look at the code (not touching it) can bug it.
Working on Office 365.
Thanks in advance.