VBA Help Please

victorski

New Member
Joined
Nov 27, 2024
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello brains trust,

I'm hoping someone can help me.

I have a workbook that I use to manage a project - I have 3 sheets, 'Overdue' (Sheet3), 'Priority Master List' (Sheet4) and 'Completed' (Sheet10)

The priority master list has activities listed in it - I created a macro that copies an entire row to the 'Overdue' when 'Days Left' is less than or equal to '5'.

Sub MoveData()
Sheet4.Range("A1:G1").AutoFilter
Sheet4.[G1:G150].AutoFilter Field:=7, Criteria1:="<=5"
Sheet4.[A1:G150].Copy Sheet3.[A1]
Sheet4.[A1].AutoFilter
End Sub

This seems to work good for my purposes.

I would also like the macro to move rows to the 'Completed' sheet and then delete it from the 'Master Priority List' when a date inputted into the 'END' field.

Could anyone please help me with this?

I also have a manager who reviews my workbook and doesn't know how to run the macro - is it possible to program it run automatically rather than manually running it?

Any help would be greatly appreciated.

Victor

Priority Master List Mini-sheet:

IDLMP Works Schedule EXTRA TEST.xlsm
ABCDEFGHI
1PriorityTaskCommentsASSIGNED TOPROGRESSDUEDAYS LEFTSTARTEND
2
3110%31/12/202433
41.10%15/12/202417
5110%31/12/202433
61.20%15/12/202417
71.20%20/01/202553
81.20%20/01/202553
91.20%15/12/202417
101.30%20/12/202422
111.40%31/12/2025398
121.40%15/12/202417
131.40%20/01/202553
141.40%20/01/202553
151.40%15/12/202417
161.40%1/03/202593
171.55%31/12/202433
181.50%15/12/202417
191.50%20/01/202553
201.50%20/01/202553
211.595%15/12/202417
221.50%15/11/2024-13
231.50%15/11/2024-13
241.520%6/11/2024-22
251.510%30/10/2024-29
261.50%23/11/2024-5
27
282.10%31/12/202433
292.10%7/11/2024-21
302.10%20/01/202553
312.20%31/12/20271128
322.30%31/12/20271128
332.40%31/12/202433
342.50%31/12/20271128
35
363.10%12/03/2025104
373.20%11/02/202575
383.30%3/03/202595
39
404.10%3/12/20245
414.10%8/10/2024-51
424.10%8/11/2024-20
434.20%
444.30%
454.40%3/12/20245
464.40%8/10/2024-51
474.40%8/11/2024-20
484.50%14/01/202547
494.60%
505
515.1100%24/07/202424/07/2024
525.20%3/03/202595
535.295%15/10/2024-4415/08/202424/10/2024
545.20%
555.20%31/10/2024-28
565.20%15/12/202417
575.290%31/10/2024-28
585.20%5/12/20247
595.20%5/12/20247
605.20%10/12/202412
615.20%20/01/202553
625.30%3/03/202595
636
646.190%4/12/20246
Priority Master List
Cell Formulas
RangeFormula
G3:G26,G64,G55:G62,G52:G53,G45:G48,G40:G42,G36:G38,G28:G34G3=F3-TODAY()
Cells with Conditional Formatting
CellConditionCell FormatStop If True
I5Expression=AND($G5<>"",$G5<=5)textNO
I3Expression=AND($G3<>"",$G3<=5)textNO
I10Expression=AND($G10<>"",$G10<=5)textNO
I4Expression=AND($G4<>"",$G4<=5)textNO
I6:I9Expression=AND($G6<>"",$G6<=5)textNO
E64Other TypeDataBarNO
E64Expression=AND($G64<>"",$G64<=5)textNO
E28Other TypeDataBarNO
E31:E34Other TypeDataBarNO
E55:E56Other TypeDataBarNO
E57:E62,E51:E54Other TypeDataBarNO
E29:E30Other TypeDataBarNO
D67:I154,D14:I63,D3:H13,I11:I13Expression=AND($G3<>"",$G3<=5)textNO
E18:E20Other TypeDataBarNO
E21:E26,D63,E35:E49,E3:E17,E54Other TypeDataBarNO



Overdue Mini-sheet:
IDLMP Works Schedule EXTRA TEST.xlsm
ABCDEFG
1PriorityTaskCommentsASSIGNED TOPROGRESSDUEDAYS LEFT
21.50%15/11/2024-13
31.50%15/11/2024-13
41.520%6/11/2024-22
51.510%30/10/2024-29
61.50%23/11/2024-5
72.10%7/11/2024-21
84.10%3/12/20245
94.10%8/10/2024-51
104.10%8/11/2024-20
114.40%3/12/20245
124.40%8/10/2024-51
134.40%8/11/2024-20
14595%15/10/2024-44
1550%31/10/2024-28
16590%31/10/2024-28
Overdue
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E15Other TypeDataBarNO
E14,E16Other TypeDataBarNO
E7Other TypeDataBarNO
D19:G104,D2:G16Expression=AND($G2<>"",$G2<=5)textNO
E8:E13,E2:E6Other TypeDataBarNO


Completed Mini-sheet:
IDLMP Works Schedule EXTRA TEST.xlsm
ABCDEFGHI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Completed
 

Attachments

  • VBA Help.PNG
    VBA Help.PNG
    23.9 KB · Views: 2

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hello all,

I have 3 worksheets, 'Master List' (Sheet4), 'Completed' (Sheet10) and 'Overdue' (Sheet3).

I work out of the Master List which has a list of tasks.

I have tried putting together two macros, but I desperately need some help.

I want Macro 1 to run when the workbook is opened or when the date in 'G' is changed - I want the macro to determine if the value in 'Days Left' (J) is <= 5, and then copy the entire row to Overdue 'Sheet3'

This is the macro I wrote:

Private Sub Workbook_Open()

Sheet4.Range("A1:J1").AutoFilter
Sheet4.[G1:G150].AutoFilter Field:=7, Criteria1:="<=5"
Sheet4.[A1:J150].Copy Sheet3.[A1]
Sheet4.[A1].AutoFilter

End Sub

This macro works to move them from Sheet4 to Sheet3, but is only triggered when I open the notebook not when a value in 'G' is changed.

I want Macro 2 to run anytime 'YES' is entered into Completed 'J' - I want it to move the entire row to Completed 'Sheet10', and delete the row from Master List (Sheet4) and Overdue (Sheet3).

This is the Macro I wrote:

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("J:J")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Target.EntireRow.Copy Sheets("Completed").Cells(Sheets("Completed").Rows.Count, "A").End(xlUp).Offset(1)
Target.EntireRow.Delete
Application.EnableEvents = True

End Sub

I couldn't figure out how to get the macro to activate when I typed YES so now it activates as soon as anything is entered into the field - also I can't seem to insert new rows into the Sheet4 as it just moves them straight to Sheet10.

Please can anybody help?
 

Attachments

  • New VBA Help.PNG
    New VBA Help.PNG
    50.6 KB · Views: 4
Upvote 0
You have to add another If statement.
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
    If .CountLarge > 1 Then Exit Sub
    If .Column = Range("J:J").Column Then
        If .Value = "Yes" Then
            Application.EnableEvents = False
            Target.EntireRow.Copy Sheets("Completed").Cells(Sheets("Completed").Rows.Count, "A").End(xlUp).Offset(1)
            Target.EntireRow.Delete
            Application.EnableEvents = True
        End If
    End If
End With
        
End Sub
 
Upvote 0
Also, if you want to Paste to multiple locations then (as an example):

VBA Code:
Selection.EntireRow.Copy
Sheets("Sheet1").Cells(1,1).PasteSpecial
Sheets("Sheet2").Cells(1,1).PasteSpecial
Application.CutCopyMode = False
 
Upvote 0
Moreover, you can remove the original data by using

VBA Code:
Selection.EntireRow.Cut Sheets("Sheet1").Cells(1,1)

But really you don't have to use Copy or Cut at all. You can just

VBA Code:
Sheets("Sheet1").Rows(Selection.Row).Value = Sheets("Sheet2").Rows(1).Value
 
Upvote 0
Thank you so much - that has fixed inserting new rows!! Yay :)

I'm sorry, I'm struggling to figure out where to put those additional lines of code?

I put the first macro you gave into the code for "Master Priority List"

Now If I put the following macro into a module and run it, the row disappears as it no long exists on the Priority Master List.

Sub MoveData()
Sheet4.Range("A1:J1").AutoFilter
Sheet4.[G1:G150].AutoFilter Field:=7, Criteria1:="<=5"
Sheet4.[A1:J150].Copy Sheet3.[A1]
Sheet4.[A1].AutoFilter
End Sub


Would you be able to help with the Macro so that it automatically updates when the workbook is opened and if the date in "G" is changed and also delete the entry once it is removed from 'Priority Master List' sheet?

Thank you again for your time, I really appreciate it.
 
Upvote 0
I think I understand what you want. Try this on a copy of your Workbook.

Insert a new Module and re-name it SortWB. Paste the following code into it.

VBA Code:
Option Explicit

Sub DaysLeftChange()

Dim wb As Workbook, msht As Worksheet, osht As Worksheet, rng As Range, lr As Long, i As Long

Set wb = ThisWorkbook
Set msht = wb.Sheets("Priority Master List")
Set osht = wb.Sheets("Overdue")
osht.Cells.ClearContents
Set rng = Range(msht.Cells(1, 1), msht.Cells(lRow(msht, msht.Rows.End(xlDown).Row), msht.Columns.End(xlToRight).Column))
rng.AutoFilter Range("C:C").Column, "Troy"
For i = 1 To rng.Rows.Count
    If rng.Rows(i).Hidden = False Then
        Range(osht.Cells(lRow(osht, osht.Rows.End(xlDown).Row) + 1, 1), osht.Cells(lRow(osht, osht.Rows.End(xlDown).Row) + 1, _
            rng.Columns.Count)).Value = rng.Rows(i).Value
    End If
Next i
rng.AutoFilter

End Sub
Private Function lRow(ByRef sht As Worksheet, ByVal last_row As Long) As Long

Static cnt As Integer
If last_row = sht.Rows.Count And IsEmpty(sht.Cells(1, 1)) Then lRow = 0: Exit Function
If last_row = sht.Rows.Count And Not IsEmpty(sht.Cells(1, 1)) Then lRow = 1: Exit Function
lRow = sht.Rows.End(xlDown).Row

End Function

The following code will be pasted into the Priority Master List Sheet code module

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
    If .CountLarge > 1 Then Exit Sub
        If .Column = Range("G:G").Column Then
           SortWB.DaysLeftChange
        End If
    End If
End With
With Target
    If .CountLarge > 1 Then Exit Sub
    If .Column = Range("J:J").Column Then
        If .Value = "Yes" Then
            Application.EnableEvents = False
            Target.EntireRow.Cut Sheets("Completed").Cells(Sheets("Completed").Rows.Count, "A").End(xlUp).Offset(1)
            Target.EntireRow.Delete
            Application.CutCopyMode = False
            Application.EnableEvents = True
        End If
    End If
End With
        
End Sub

The following code will be pasted into ThisWorkbook code module

VBA Code:
Private Sub Workbook_Open()
SortWB.DaysLeftChange
End Sub
 
Upvote 0
Thank you Skybott, I really appreciate your help :) I have pasted in the codes where you have suggested.

When the Macro runs it shows the following errors, then it activates the filter on column C.

Any ideas what I might have done wrong?
 

Attachments

  • Screen Shot 2024-11-30 at 6.22.45 pm-min (1).png
    Screen Shot 2024-11-30 at 6.22.45 pm-min (1).png
    114.9 KB · Views: 2
  • Screen Shot 2024-11-30 at 6.22.55 pm-min.png
    Screen Shot 2024-11-30 at 6.22.55 pm-min.png
    85 KB · Views: 1
  • Screen Shot 2024-11-30 at 6.24.44 pm-min.png
    Screen Shot 2024-11-30 at 6.24.44 pm-min.png
    65.9 KB · Views: 2
  • Screen Shot 2024-12-01 at 11.15.07 pm-min.png
    Screen Shot 2024-12-01 at 11.15.07 pm-min.png
    121.6 KB · Views: 2
Upvote 0
Hi Skybott, apologies to write again and to take so much of your time.

I tried your instructions again and started getting 'Compile error: End If without block if' on Private Sub Worksheet_Change so I removed a duplicate End If statement and it seems to be okay now.
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
    If .CountLarge > 1 Then Exit Sub
        If .Column = Range("G:G").Column Then
           SortWB.DaysLeftChange
        End If
End With
With Target
    If .CountLarge > 1 Then Exit Sub
    If .Column = Range("J:J").Column Then
        If .Value = "Yes" Then
            Application.EnableEvents = False
            Target.EntireRow.Cut Sheets("Completed").Cells(Sheets("Completed").Rows.Count, "A").End(xlUp).Offset(1)
            Target.EntireRow.Delete
            Application.CutCopyMode = False
            Application.EnableEvents = True
        End If
    End If
End With
        
End Sub

Now if I put yes in column J it moves it to 'Completed' which is perfect - but it leaves the now empty cells on 'Priority Master List' can these be removed automatically?

Also, if I put any figures less than or equal to 5 into column Days Left 'G' it doesn't move them to the 'Overdue' sheet, any ideas why this is?

If it helps the due 'F' column has a date, then the 'G' column (Days Left) has '=F1-TODAY()'

I'm not sure if it makes any difference but I have those columns headers replicated on all 3 sheets, so the actual data doesn't start till A2:J2

Thank you again for all your help - if I can do anything to assist please don't hesitate to ask.
 
Upvote 0
I want Macro 1 to run when the workbook is opened or when the date in 'G' is changed - I want the macro to determine if the value in 'Days Left' (J) is <= 5, and then copy the entire row to Overdue 'Sheet3'
1) To Sheet4 worksheet code module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns("g")) Is Nothing Then
        Range("A1:J1").AutoFilter
        [G1:G150].AutoFilter Field:=7, Criteria1:="<=5"
        [A1:J150].Copy Sheet3.[A1]
        [A1].AutoFilter
    ElseIf Not Intersect(Target, Columns("I")) Is Nothing Then
        Application.EnableEvents = False
        Target.EntireRow.Copy Sheets("Completed").Range("a" & Rows.Count).End(xlUp)(2)
        Target.EntireRow.Delete
        Application.EnableEvents = True
    End If
End Sub
2) To ThisWorkbook code module
Code:
Private Sub Workbook_Open()
    Run "Sheet4.worksheet_change", Sheet4.[g1]
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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