Find Last Row of Name with constantly changing names - vba

I'm really stuck on this one. I have a spreadsheet with thousands of rows. I use this code to filter them based off of product in the E column.
Sub IsolateCCENCE()
Dim Operations As Workbook
Dim Operations_Sheet As Worksheet
Set Operations = Workbooks("Operations for Macros")
Set Operations_Sheet = Operations.Worksheets("Operations")
Operations_Sheet.Range("$A$6:$AH$13108").AutoFilter Field:=5, Criteria1:="=CCE" _
, Operator:=xlOr, Criteria2:="=NCE"
End Sub
Which works and leaves me with just under 1700 rows. Within these rows, in the A column, there are company names. Each company takes up approximately 20 rows. Each row represents a payment and has a corresponding date, in the D column. I need a macro (I'm assuming with a loop) that will then do the following:
Go through the rows, find the last row for each company
In that row, find the corresponding date
If that date is within 30 days from today, generate an email
Part 3 is easy. But Part 1 and 2 I can't seem to get. The data is always going to be changing.
Maybe it would be easier to have all of the data copy and pasted into another spreadsheet and then filter through every single company, find the last row (and thus the corresponding date)? But I don't know I would have a macro defined to filter through each company when the company names will be changing constantly.
I appreciate any help. Thanks in advance!

If a specific company name in say F1 then:
=MIN(IF(A:A=F1,D:D))
entered with Ctrl+Shift+Enter should give you the earliest date for the company named in F1, that if more recent than today()-30 (or less far into the future than today()+30 ?) you might use for your e-mail trigger (subject to other filtering etc).

Related

Extract Top 10 products from a Excel Database (Multiple criteria)

I have a database (example attached) that gets updated automatically from other 70 files on monthly bases by macro.
I would like to extract Top 10 ordered products based on various criteria.(example attached) The file will be more complex, just tried to keep it simple for demo.
I have used some formula =LARGE('Part numbers'!A3:A301,ROW(INDIRECT("1:"&ROWS('Part numbers'!A3:A301)))) & =SUMPRODUCT((Combine!B1:B10000='DATA '!Y3)*(Combine!C1:AB1=A1)*(Combine!D1:D10000=AC2),Combine!C1:AB10000)that aloud me tho get the SUM of top5 products the problem is when I use the INDEX MATCH to extract the name of the product if there are 2 same values the formula is stooping always at the first product name + I need to add a new criteria witch is the month
Thank you in advance for any suggestions
Its ok if it is macro or formula
.
Trying fixing the range by making reference using the INDIRECT-function:
=SUMPRODUCT((INDIRECT("Combine!$B$1:$B$5000")='DATA '!Y3)*(Combine!$C$1:$AB$1=$C$1)*(INDIRECT("Combine!$D$1:$D$5000")=$AC$2),INDIRECT("Combine!$C$1:$AB$5000"))
This ensures that the Range you are referencing, e.g. $B$1:$B$5000 doesn't become $B$1:$B$4999 when for example Row 2 is deleted.
maybe the pivot-table can help u?
there some autofilters to show the top x
if u want to set a dynamic filter on pivot table u can use this:
Sub UpdateFilter()
Dim m As String
m = Month(Now())
[filPivot] = m
End Sub
Issue solved more less.
The other problem that I have now is with the formula =SUMPRODUCT((Combine!$B$1:$B$5000='DATA '!Y3)*(Combine!$C$1:$AB$1=$C$1)*(Combine!$D$1:$D$5000=$AC$2),Combine!$C$1:$AB$5000) initially I have the range of the formula =5000 but every time when my database is updated this number is decreasing till it reaches =10.
My database is updated by a macro that deletes Blanks every time,I believe this could be the issue.
Any ideas how to make this =5000 range steady?

Deleting all columns between the first one and the last four on a sheet

I have a worksheet here with multiple columns full of data, ranging from cell AA to CT currently. The first column contains rows with headings and is frozen. Data is added to the end column of this range weekly. & Then all columns between the first and the last four are deleted (to show a month's worth of data but keep the headings intact).
I'm very new to VBA and I've been trying to write some code to automate this, so far I've managed to select the 5th column in from the end. But I can't get it to select the columns in between the 4th column from the end and Cell AA:
Sub DeleteColumns()
Dim i
i = Range("KFIData").End(xlToRight).Offset(0,-4).EntireColumn.Select
Columns("AA:ColumnFive").Delete
End Sub
Am I going about this completely the wrong way?
Many thanks
Well if you managed to catch column 5 from the end, use this statement:
Range(Columns("AA"), Columns(ColumnFiveFromEnd)).Delete
ColumnFiveFromEnd can be a number as well as a text identifier.

Insert text in cells when above cell meets condition

I have a schedule sheet for the whole year for a group.
The third row has all the dates of the year, the next couple of rows has the name of each group member.
Members specified in A4:A9.
Holidays are specified in cells A20:A28.
So, what I want to do, probably through VBA, is to make a rule like with conditional formatting, that inserts the word "Holiday" in B4:B9 if B3 matches a date in A$20:A$28, and then into C4:C9 if C3 matches and so on.
Now I cannot write this condition in every cell since it's a schedule used by every member, which is the suggestion I often got from googling, and I can't do it with conditional formatting, so I'm guessing VBA is the way to go, but the VBA code I've found so far seems to use specific cells and wouldn't go through each column.
Without questioning your general approach, a simple code that does what you plan to do could work like the following:
Option Explicit
Sub markhd()
' get calendardays, holidays and employees somehow into collections
' that can be looped. Since that information is stored in a worksheet,
' using ranges is one way to achive that
' holidays
Dim rhd As Range
Set rhd = ActiveSheet.Range("A20:A28")
' group employees
Dim remp As Range
Set remp = ActiveSheet.Range("A4:A9")
' calendar days
Dim rcal As Range
Set rcal = ActiveSheet.Range("B3:NB3")
Dim c As Object
' Loop every day (every c-object in your calendar days),
' then write "Holiday", if it matches an entry in your holiday-range
For Each c In rcal
If Not IsError(Application.Match(c, rhd.Cells, False)) Then
remp.Offset(0, c.Column - remp.Column).Value = "Holiday"
End If
Next c
End Sub
Note:
The concept #EganWolf mentioned is to look for similar objects (calendar days in this example) and iterate through them, performing an action (writing "Holiday") if a check is passed (calendarday is in the range of holidays). There is a gazillion of different ways implement this, i merely posted one easy to follow example
Looping each and every calendar day we're doing 365 iterations here, attempting to match each calendarday in the 9 holidays.
However, we could do this the other way around: loop the 9 holidays, match them in the 365 calendar days, therefore looping only 9 times. Performance could theoretically swing both ways, since despite having less iterations, a single iteration could take longer... but, obviously, at 365 days performance is merely a theoretical problem.
Hope that helps!

Excel Compare two sheets and update sheet 1

Okay - This has been asked multiple times, but asking again for best possible solution :
I have two excel files (not sheets). the first excel sheet is very huge and has close to 200,000 records. One of the column (Gender) is corrupted and i have to fix it.
I have a second excel file and it has only around 200 records - these have the correct value for those ones which are messed up.
for eg:
and this is the file that has correct values with only around 200 records (only the corrupted ones).
Now i need a macro , where i need to find these exact 200 records out of 200,000 records (by employee id) and replace the Gender value with correct one.
i found something similar here. but i dont want to loop 200,000 records 200 times. feels like a performance overhead.
is there a better option?
I am thinking an ideal solution would be
Loop through 200 items and use employee id per loop
Take that employee id and do a "Find" operation in the Employee id column of the master excel
If found, replace the Gender column value
would there be any other better solution? Any inputs is gladly appreciated
One way to do this through VBA is to just loop through the 200 corrections, comparing the ID with the MATCH function to find the row it belongs on, as opposed to a second loop (a second loop through 20000 would take ages like you say).
For the below sub I have copied and pasted the 200 table into columns 5:7 of the 20000 table, you can either automate this part easily enough, or just put in the correct sheet references for each part of the code.
I've also put in a checking line to make sure there IS a match for the current ID from the small table, otherwise it'd throw up an error. You could put an ELSE in front of the END IF in this error catch to highlight any ID's which weren't actually found. Here's the code, hope this method helps!
Sub replace_things()
With ActiveSheet
For x = 2 To 200 'Change this to however many is in the small table
cur = .Cells(x, 5) 'Defined cur as ID from small table
aMatch = Application.WorksheetFunction.CountIf(.Range("A:A"), cur) 'Check to see there's a match in large table
If aMatch > 0 Then ' if there's a match then...
theRow = Application.WorksheetFunction.Match(cur, .Range("A:A"), 0) 'get the row number the match is actually on
.Cells(theRow, 3) = .Cells(x, 7) 'when row is found, replace with the relevant value from col7 (col3 of small table)
End If
Next x
End With
End Sub
A super quick way, copy your CORRECT employee ID list and paste below the CORRUPT employee ID list... highlight duplicates and correct the highlighted.
Otherwise a VLOOKUP could label which ones are corrupt? basically getting a unique field from your correct list and comparing that to your corrupt list then fixing the ~200 errors.
I assume that the employee ID is a unique record so you can paste the correct ones under existing ones, sort by empID and highlight duplicates to find them easily.

Index and Match Excel

I'm creating a vacation tracker in Excel. The first sheet pulls the data from SharePoint which has Start Date, End Date and a Date Difference calculation.
Sheet1
A2=12/16/2015
B2=12/20/2015
C2=5
The second sheet is the visualization of the data. It starts with cell B1 and goes out for 90 days. Is is a word representation of the date. Here is the formula I'm using =UPPER(TEXT(B2,"DDD"))
Sheet2
B1=WED
C1=THU
D1=FRI
E1=SAT
F1=SUN
The next row always B2 has a formula which is always today's date. From there I add one date to increment the dates out to 90 days. B2=Today() and the other cells =B2+1 and so forth
B2=16
C2=17
D2=18
E2=19
F2=20
The problem I'm running to is that our boss can have 3 calendar entries consisting of different dates. So when I perform a SharePoint pull I have 3 different rows of vacation dates. I'm experimenting with an Index and Match example, however the data match it is placed in a new row. How do I place the data on the same row?
I'm not convinced this has much to do with VBA. Seems enough detail of final design but rather light on the data gathering process. However when I perform a SharePoint pull I have 3 different rows of vacation dates hints at a use for Subtotal. Three rows may be 'converted' into one:
where the unshaded part (some cells with Xs) is assumed to be a representation of the current SharePoint data extract. Subtotal is able to add the rows shown lightly filled, which might then be used to =VLOOKUP("Boss"&" Count",A:F,n,0) where n represent the choice of column label.