I'm a business student who just started to learn VBA. I am trying to write a macro for a project but only have minimal experience actually stepping into the code. What I am trying to do is delete all row entries which have future month's dates in them, and I'd like for this to update based on the current month. I hope I am using the correct functions, but maybe just in the wrong order.
I'm trying to compare the data for each row (in this case I'm looking at cell 16 in each) and I was thinking that if the month digits for the date in that column are greater than the value of the month digits for the current month, then it should delete, but I'm receiving the error [Run-time error '5' Invalid procedure call or argument].
So here is the part of the code I am having trouble with:
If DatePart(mm, Cells(iCntr, 16)).Value > DatePart(mm, Date).Value Then
Rows(iCntr).Delete
In the code, I was only focusing on the month portion because the file I'm using only contains current year information, so I wouldn't have to worry about accidentally failing to delete something for March of next year (03/2017) due to it being June of this year (for example, technically 03/13/2017 would not be deleted since 03 < 06).
(Second question for my own learning experience--someone suggested I use iCntr in this, but what does this actually do for the formula?)
Update: Went to Code Review and they updated my code and now the problem I am running into is that the friend who sent me the file left some rows of blanks that contain a single space, which is causing an error when I run the macro. Could someone suggest how to use the trim() function to eliminate those?
Sub Remove_excess_entries()
Application.ScreenUpdating = False
Dim lRow As Long
Dim iCntr As Long
lRow = 10000
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 12).Value = "Mule" Or Cells(iCntr, 11).Value = "*R1*" Or Cells(iCntr, 11).Value = "*R2*" Or Cells(iCntr, 7).Value = "*Mule*" Or Cells(iCntr, 6).Value = "*Unassigned*" Or Cells(iCntr, 12).Value = "PS" Or Cells(iCntr, 7).Value = "Marketing" Or Cells(iCntr, 12).Value = "V1" Or DatePart("m", Cells(iCntr, 16).Value) > DatePart("m", Date) Then
Rows(iCntr).Delete
End If
Next
Application.ScreenUpdating = True
End Sub
iCntr isn't a VBA command, it's likely just a declared variable (possibly a counter value for iterating over the range of rows you want to consider, based on the context of the code shown). Presumably you have it declared somewhere and defined in a For type loop?
Secondly, according to MSDN the correct parameter for "month" in DatePart is "m", not mm. That means your code should read as:
If DatePart("m", Cells(iCntr, 16).Value) > DatePart("m", Date) Then Rows(iCntr).Delete
You also have the .Value outside the closing brackets, which means you are trying to assign it to the DatePart object rather than the Cells one. Feel free to ask anything further if I've not explained it well enough
as for the correct use of DatePart() function, type:
If DatePart("m", Cells(iCntr, 16)) > DatePart("m", Now) Then Rows(iCntr).Delete
Related
I'm struggling with the following: I want to delete rows for which date (column C) is prior than today. My code should work (according to the web) but it doesn't and moreover, it's super slow.. Here is a part of it:
For i = 2 To LastRow
If Cells(i, "C").Value < Date Then Rows(i).EntireRow.Delete
Next i
Instead of putting "C", I could put 3, but doesn't change anything. I've tried to add the End If (before the next i), but not necessary apparently. Maybe the Date is not the right format..
What's wrong with this code ?
Great thanks for your help :)
Use the auto-filter capabilities of Excel in your favor. Filter the range by your criteria to delete the matching rows:
With Range("C1:C" & lastrow)
.AutoFilter 1, "<" & CLng(Date)
.Offset(1).EntireRow.Delete
.AutoFilter
End With
You will want to step backwards when deleting rows and verify that it is actually a date in the cell. To see what it's doing, set a breakpoint on the first line and use F8 to step through it. Also verify the value of LastRow is getting set to the correct value.
For i = LastRow To 2 Step -1
If IsDate(Cells(i, "C").Value) then
If Cells(i, "C").Value < Date Then Rows(i).EntireRow.Delete
End If
Next i
I have a csv file on excel that is ~CU for the column.
And the row gets keep updated (as of now it's 2606).
I'm trying to
delete all row that are before today's date as recorded on column D
no typing/text box/human input for today's date.
Sub deleterows()
lastrow = Cells(Rows.Count, 4).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 4).Value < *numeric value* Then Rows(i).EntireRow.Delete
Next i
End Sub
For dates (and currency), its always recommended to use Value2 instead of Value.
MSDN:
The only difference between this property and the Value property is
that the Value2 property doesn’t use the Currency and Date data types.
You can return values formatted with these data types as
floating-point numbers by using the Double data type.
So all you need to do is change this part If Cells(i, 4).Value < *numeric value* Then
With this If Cells(i, 4).Value2 < Date Then
and it evaluate as true if Column D is older than today.
Working with excel-Access VBA environment. The vba code in the excel page assigns the value of a date cell to the date filter. Since I always work dd/mm/yyyy format with panama locale, the code works fine for dates after the 12th (meaning there's no ambiguity to designate the month), but for days les than 13, it converts the day to its numeric value and I get an error message saying 42502 (for may 12,2016 for example) is not a valid value for the filter. When the day passes the 12th, it works fine. How can i trap this error and solve it ?
Code :
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
' Range R1 contains the value we desire to filter the date of dynamic table
' Range B1 contains the date filter of the dynamic table
Range("B1").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("fecha").ClearAllFilters
Application.ScreenUpdating = False
'ActiveSheet.Range("B1") = Range("R1").Value
ff = Range("R1").Value
ActiveSheet.Range("B1") = Right("00" & Day(ff), 2) & "/" & Right("00" & Month(ff), 2) & "/" & Year(ff)
Application.ScreenUpdating = True
This is one of the many ways Ive tried to solve this, but only works for days after the 12th of the month
It would be helpful if you posted some code, but I believe the problem is you need to specify in code what format you are using instead of letting VBA guess it for you. Since Microsoft is a US company and we use the MM/DD/YYYY format, it could be defaulting to the US format, but then when it reaches the 13th, it defaults to the non-US format by the context of the date.
Try this:
Dim lastRow, i As Long
Dim datStg As String
lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 2 To lastRow
dateStg = Cells(i, "R").Text
If datStg <> "" Then
Cells(i, "B").Value = DateSerial(Mid(dateStg, 8, 4), Mid(dateStg, 5, 2), Mid(dateStg, 2, 2))
Cells(i, "B").NumberFormat = "dd/mm/yyyy"
End If
Next i
Hi and good day everyone,
Basically I wish to extract the data based on the number of days per month. Please find the link below for better illustration:
https://drive.google.com/open?id=0B15JqLrOZtewamlRQ1ZMTmJZNWc&authuser=0
For example, D1 shows the current month is September. Therefore I will only need to extract the data from day 1-30. Also I will need to copy the ID and paste it at left column of the date for 30 times.
Any idea?
Thanks for your time!
Cheers,
Patch
I'll get you started on the macro code necessary to determine the number of days within a month when the month is spelled out as text.
Dim dys As Long
With ActiveSheet
dys = Day(Application.EoMonth(DateValue(.Cells(1, 4).Value & " 1, " & Year(Date)), 0))
.Cells(2, 7).CurrentRegion.ClearContents
.Cells(2, 7) = "ID"
.Cells(2, 8).Resize(1, 4) = .Cells(2, 1).Resize(1, 4).Value
.Cells(3, 7).Resize(dys, 1) = .Cells(1, 2).Value
.Cells(3, 8).Resize(dys, 4) = .Cells(5, 1).Resize(dys, 4).Value
Debug.Print dys
End With
With the last day of the month determined, it is a small matter to use that to resize the source/target regions of cells. I tried to determine the best guess for the cell addresses from your sample but the merged areas may have fouled things up.
I'm new to this site as well as to VBA. I've been working on a project and have run into a wall. I hope someone can help me out. What I'm trying to do is create a loop that will go through the specified sheet and pull data that matches my criteria, copy and paste it to another sheet, where I will be calculating it and then showing the results of the calculations in another sheet.
so, I have the following code (excel VBA) that is suppose to go through the sheet and pull all records that match the current week (I'm also trying to add the current year, no luck so far) and paste all matching records to the sheet named Archieve:
Sub DataByWeek()
Dim cw As Integer ' current week
Dim cy As Integer ' current year
Dim lr As Long 'last row of data
Dim i As Long ' row counter
'Get week number of today's date
cw = Format(Date, "ww")
cy = Format(Date, "yyyy")
ActiveWorkbook.Worksheets("Daily DB").Activate
' Find last row of data plus one down
lr = Sheet7.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 2 To lr
If Cells(i, 6) = cw Then
Range(Cells(i, 1), Cells(i, 5)).Copy
ActiveWorkbook.Worksheets("Archieve").Activate
Range("A2").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
Next i
End Sub
This code does everything I want it to do except return beyond the first matching iteration. The code does go through all data on the sheet named Daily DB, but only returns the first matching record. I've tried looking online (many site including this one) to see if I missed something or did something wrong, but I can't find where I went wrong.
I would also like to know how I can add a second criteria to the If statement condition. I'd like to add the year so that the condition reads something like
If Cells(i, 6 & 7) = cw & cy Then
...
Where i, 6 contains the week number and i, 7 contains the year. In other words, I'd like to 'say' find all records that contain the x week of x year.
Sorry if this was too long and thank you in advanced for any and all help.
If you add ActiveWorkbook.Worksheets("Daily DB").Activate before the End If statement, I believe it would fix your problem. I'm not sure it's the most efficient way though.