Extend deadline for today's dates - excel

I use the following VBA to extend the deadline of over-due (due today) tasks in the end of the day. However, I realized that applying the script twice (I linked the script to a button, which I accidentally pressed twice) results in all task-dates (and also the tasks with no date assigned) to be repalaced by tomorrow's date or get a date (next day).
How can I avoid this unwanted behavior? It seems the selection process of the dates to be changed is distored when applying the script twice.
Sub To_Do_Add_Day_Deadline()
'
' To_Do_Add_Day_Deadline Makro
'
'
Range("C2").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3, Criteria1 _
:=xlFilterToday, Operator:=xlFilterDynamic
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("C4").Select
ActiveCell.FormulaR1C1 = "=TODAY()+1"
Range("C4").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("E6").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("E5").Select
End Sub

I replace Range("C4") with Range("C2"), because when you apply a filter, the table constraint their rows, and ever start at the next line after the header Range(""). I test this in a Table with a Range("A1:E25") with a header.
Sub To_Do_Add_Day_Deadline()
Range("C2").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3, Criteria1 _
:=xlFilterToday, Operator:=xlFilterDynamic
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection = CDate(Left(CDate(Now) + 1, 10))
Application.CutCopyMode = False
Range("E6").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3
End Sub
Edit: I reduce the code and replace "=TODAY()+1" with CDate(Left(CDate(Now) + 1, 10))

I changed the approach and now use the following code, which works fine:
Sub On_Hold_Add_One_Day()
'
' On_Hold_Add_One_Day
'
'
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E4").Select
ActiveCell.FormulaR1C1 = "=IF([#Deadline]=TODAY(),[#Deadline]+1,[#Deadline])"
Range("E4").Select
ActiveWindow.SmallScroll Down:=-9
Range("E4").Select
ActiveWindow.SmallScroll Down:=0
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-36
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=-9
Columns("E:E").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Cells.Select
Selection.Rows.AutoFit
Range("E6").Select
End Sub

Related

Macro blanking value when only one row in spreadsheet

I have a macro that performs some cleanup on values. It works fine unless there is only one row of data in the spreadsheet; if there is only one row, it blanks out my value instead of fixing it.
Sub UpdateNumberFormat()
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Columns("AL:AM").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.NumberFormat = "General"
Range("AL2").Select
ActiveCell.FormulaR1C1 = "'"
Range("AM2").Select
ActiveCell.FormulaR1C1 = "=CONCAT(RC[-1],RC[1])"
Range("AL2:AM" & LastRow).Select
Selection.FillDown
Range("AM2:AM" & LastRow).Select
Selection.Copy
Range("AN2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("AL:AM").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.NumberFormat = "General"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"
Range("C2:C" & LastRow).Select
Selection.FillDown
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub

Macro to Copy SPECIFIC DATA to a TEMPLATE and save it as TXT file

I have macro to copy data based on certain filters and paste it into a template.
Column J to be copied to Column A.
Column C to be copied to Column B.
Column D to be copied to Column E.
Column H to be copied to Column F.
Sub DS()
Selection.AutoFilter
ActiveSheet.Range("$A$1:$O$5000").AutoFilter Field:=12, Criteria1:="PENDING"
ActiveWindow.SmallScroll Down:=-12
ActiveSheet.Range("$A$1:$O$5000").AutoFilter Field:=10, Criteria1:="U3R"
ActiveWindow.SmallScroll Down:=-36
Windows("Transfers 2020 - Roy.xlsm").Activate
Range("J469:J537").Select
Selection.Copy
Windows("template2.xlsx").Activate
Range("A1").Select
ActiveSheet.Paste
Windows("Transfers 2020 - Roy.xlsm").Activate
ActiveWindow.SmallScroll Down:=-15
Range("C469:C537").Select
Application.CutCopyMode = False
Selection.Copy
Windows("template2.xlsx").Activate
Range("B1").Select
ActiveSheet.Paste
Windows("Transfers 2020 - Roy.xlsm").Activate
ActiveWindow.SmallScroll Down:=-15
Range("D469:D537").Select
Application.CutCopyMode = False
Selection.Copy
Windows("template2.xlsx").Activate
Range("E1").Select
ActiveSheet.Paste
Range("F2:J2").Select
Application.CutCopyMode = False
Selection.Copy
Range("F3:J18").Select
ActiveSheet.Paste
Range("C1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=CONCAT(RC[2],RC[-1],RC[-2],2262020)"
Range("C1").Select
Selection.Copy
Range("C1:C18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1:L19").Select
Application.CutCopyMode = False
Selection.ClearFormats
Range("C7").Select
ChDir "H:\A\Roy\B\DS"
ActiveWorkbook.SaveAs Filename:= _
"H:\A\Roy\B\DS\DS 2.26.2020.txt", FileFormat _
:=xlText, CreateBackup:=False
ActiveWindow.Close
End Sub
Question 1 : For the Criteria "PENDING" and "U3R" I want it to go through the whole range and look for "PENDING".
Question 2: I want to add an OR condition with "U3R" For Eg. "U3R" OR "U2R"
Question 3 : I only want to select the cells which are left after the above filters are applied. What changes should I make to this ? "Range("J469:J537").Select"
Thank you in advance!

Excel VBA - Need to grab a variable size of rows

New to VBA - Recorded the macro below and every time I run it it always selects 309 rows. I am wanting the rows to be fluid (Example: Could be 400 rows, could be 10 depending on data.
Columns("H:H").Select
`Selection.Style = "Comma"
Range("I2").Select
Selection.EntireColumn.Insert
Range("H1").Select
Selection.Copy
Range("I1").Select
ActiveSheet.Paste
Range("I2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=""C"",RC[-1]*-1,RC[-1])"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I309")
Range("I2:I309").Select
Columns("I:I").Select
Selection.Copy
Range("I1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("H4").Select
Application.CutCopyMode = False
Selection.EntireColumn.Delete
Range("L7").Select
Selection.EntireColumn.Insert
Range("K1").Select
Selection.Copy
Range("L1").Select
ActiveSheet.Paste
Range("L2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RIGHT(RC[-1],4)"
Range("L3").Select
ActiveWindow.SmallScroll Down:=-12
Range("L2").Select
Selection.AutoFill Destination:=Range("L2:L309")
Range("L2:L309").Select
Columns("L:L").Select
Selection.Copy
Range("L1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("K2").Select
Application.CutCopyMode = False
Selection.EntireColumn.Delete
Range("M23").Select
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
Range("K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet2").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Range("C281").Select
Selection.End(xlUp).Select
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.NumberFormat = "m/d/yyyy"
Range("C2:D2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Range("$C$2:$D$309").RemoveDuplicates Columns:=Array(1, 2), _
There's too much going on here for me to translate all of this code for you, but I believe the word you're looking for here is dynamic. In order to replace 309 with a dynamic last row number, you'll need to change:
Selection.AutoFill Destination:=Range("I2:I309")
to
Selection.AutoFill Destination:=Range("I2:I" & Cells(Rows.Count, "I").End(xlUp).Row)
and
Selection.AutoFill Destination:=Range("L2:L309")
to
Selection.AutoFill Destination:=Range("L2:L" & Cells(Rows.Count, "L").End(xlUp).Row)
etcetera, etcetera. Also like #cybernetic.nomad said, you should read that link he shared with you.

msgBox is not showing up on successful result

I have the below set up to copy a list and paste to sheet(data). I want it to display a message when it is successful, telling me which row did it started the paste at. However, the errmsg shows instead.
Thanks in advance
Dim current As String
current = ActiveCell.Index
MsgBox current & "pasted there"
Exit Sub
errmsg:
MsgBox "failed to copy."
End Sub
full code
Sub move()
Range("A3:B3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("K3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("F3:I3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("F3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A3:G3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
On Error GoTo errmsg
Sheets("data").Select
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Dim current As String
current = ActiveCell.Index
MsgBox current & "pasted there"
Exit Sub
errmsg:
MsgBox "failed to copy."
End Sub
Welcome to SO:) One way to try to debug this would be to remove/comment-out the "On Error GoTo", then run the code. That should show you which line is generating the error.
That said, I suspect you want current = ActiveCell.Index to be current = ActiveCell.Address.

calculating network days, with some dates omitted from exceptions - Excel

The following is a macro which collects data from data we import into certain tabs. When the macro is run it filters through the data and produces a new excel book with this new data. The person who created this is no longer with us. The macro works fine except I'm trying to add another column like the one that calculates network days called days since 1st auth less parked. Im wanting to add another which gives days since the start of the information being passed to us. Ie a column call 1st instructed less parked.
Sub Runme()
'
' Macro1 Macro
' Macro recorded 22/03/2013'
'
Sheets("CCX data SORTED").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("CCX Data Raw").Select
Range("A:C,E:G").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.AutoFilter
Range("H2").Select
Range("A1:X10000").Sort Key1:=Range("H1"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("H55000").End(xlUp)(2, 1).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.ClearContents
Cells.Select
Selection.Copy
Sheets("CCX data SORTED").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("CCX Data Raw").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("CCX data SORTED").Select
Columns("X:X").Select
Selection.Insert Shift:=xlToRight
Range("X2").Select
ActiveCell.FormulaR1C1 = _
"=IF(VLOOKUP(RC[-23],'SCMT weekly data'!C[-17]:C[-11],7,FALSE)>0,(VLOOKUP(RC[-23],'SCMT weekly data'!C[-17]:C[-11],7,FALSE)),"""")"
Range("X2").Select
Selection.AutoFill Destination:=Range("X2:X5000")
Range("X1").Select
ActiveCell.FormulaR1C1 = "SCMT end Date"
Columns("X:X").Select
Selection.NumberFormat = "m/d/yyyy"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SCMT Weekly data").Select
Columns("Z:Z").Select
Selection.Insert Shift:=xlToRight
Range("Z1").Select
ActiveCell.FormulaR1C1 = "Days To Exclude 1"
Range("Z2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-2],RC[-1])=2,NETWORKDAYS(RC[-2],RC[-1],'Bank hols'!RC[-25]:R[56]C[-25]),0)"
Selection.AutoFill Destination:=Range("Z2:Z5000"), Type:=xlFillDefault
Columns("AD:AD").Select
Selection.Insert Shift:=xlToRight
Range("AD1").Select
ActiveCell.FormulaR1C1 = "Days To Exclude 2"
Range("AD2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-2],RC[-1])=2,NETWORKDAYS(RC[-2],RC[-1],'Bank hols'!RC[-29]:R[56]C[-29]),0)"
Selection.AutoFill Destination:=Range("AD2:AD5000")
Columns("Z:Z").Select
Selection.NumberFormat = "0"
Columns("AD:AD").Select
Selection.NumberFormat = "0"
Range("AH2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("AF1").Select
ActiveCell.FormulaR1C1 = "Days since first auth"
Range("AF2").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-21],R2C34)=2,NETWORKDAYS(RC[-21],R2C34,'Bank hols'!RC[-31]:R[56]C[-31]),"""")"
Range("AG2").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISNUMBER(RC[-1]),SUM(RC[-1]-(RC[-3]+RC[-7])),"""")"
Range("AG1").Select
ActiveCell.FormulaR1C1 = "Days since 1st Auth less parked"
Columns("AH:AH").Select
Selection.Insert Shift:=xlToRight
Range("AH1").Select
ActiveCell.FormulaR1C1 = "Still Parked"
Range("AH2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(ISNUMBER(RC[-10]),ISBLANK(RC[-9])),""XX"",IF(AND(ISNUMBER(RC[-6]),ISBLANK(RC[-5])),""XX"",""""))"
Selection.AutoFill Destination:=Range("AH2:AH5000")
Range("AF2:AG2").Select
Selection.AutoFill Destination:=Range("AF2:AG5000")
Range("A55000").End(xlUp)(2, 1).Select
Selection.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("CCX data SORTED").Select
Range("Z1").Select
ActiveCell.FormulaR1C1 = "SCMT Queue"
Range("Z2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-25],'SCMT Daily Drop'!C[-16]:C[-14],3,FALSE)"
Selection.AutoFill Destination:=Range("Z2:Z5000")
Range("AA1").Select
ActiveCell.FormulaR1C1 = "Days Since First Approved"
Range("AA2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,'SCMT weekly data'!C7:C34,27,FALSE)"
Selection.AutoFill Destination:=Range("AA2:AA5000")
Range("AB1").Select
ActiveCell.FormulaR1C1 = "Still Parked"
Range("AB2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,'SCMT weekly data'!C7:C34,28,FALSE)"
Selection.AutoFill Destination:=Range("AB2:AB5000")
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A55000").End(xlUp)(2, 1).Select
Selection.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("SCMT weekly data").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(Array("SCMT weekly data", "SCMT Daily Drop", "CCX data SORTED")).Select
Sheets("CCX data SORTED").Activate
Sheets(Array("SCMT weekly data", "SCMT Daily Drop", "CCX data SORTED")).Copy
Windows("SCMT Parked.xls").Activate
Sheets("SCMT weekly data").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("SCMT Daily Drop").Select
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("CCX data SORTED").Select
Cells.Select
Selection.Delete Shift:=xlUp
MsgBox ("Macro Complete")
End Sub
Where do you want it? IF you want it in the z column, for example, try:
Columns("Z:Z").Select
Selection.Insert Shift:=xlToRight
Range("Z1").Select
ActiveCell.FormulaR1C1 = "New formula"
Range("Z2").Select
ActiveCell.FormulaR1C1 = "=AA2-AB2"
Selection.AutoFill Destination:=Range("Z2:Z5000")
I don't understand the formula you need, but there's a template you can use... If you give us the formula we can put it the second last line?

Resources