Change value of second and following duplicates - excel

I have 2 columns of data. As shown in the image below, first column has a list of duplicates and second column has the first day of the month as date. The first duplicate should be remained as 01-11-19, whereas the next duplicate should have 1 to be added in the cell to make it 02-11-19, followed by the other duplicates. How do I code this in VBA?
I tried this function, but it's not working as expected since it's modifying both the duplicates, but I only want the next duplicate to be modified.
Dim Rng, cel As range
Set Rng = .range(.Cells(firstrow, 1), .Cells(lastrow, 2))
For Each cel In Rng
If WorksheetFunction.CountIf(Rng, cel.value) > 1 Then
WorksheetName.Cells(cel.row, 2).value = WorksheetName.Cells(cel.row, 2).value + 1
End If
Next cel
Column 1 with duplicates and column 2 to be added 1 to make it next following date

Here would be a formula approach, assume Column A is the duplicated data and column B the dates. Row one contains the headers and the required info is from row 2 onwards
First, sort the data, column B oldest to newest, then Column A smallest to largest
In cell C2, copy back the date from cell B2 (as this is the first data row, it will be the same date)
In cell C3, input the formula =IF(A2=A1,B2+1,B2), and copy it down to the final row of the raw data
You can script the same into vba, by recording the steps above and fine tuning where necessary

Related

How to fill blank cells in Excel with the date of 30/06/YEAR by picking the year from the cell in the next column

Consider the following table:
I have a series of blank cells with missing data. From this missing data I only have the year in the next column. I need to fill any blank cells with a standard day/month of 30/06. The year of each cell however needs to be the year in the next column. The attached file shows how my data is arranged. So at cell B 2091, the date shall be 30/06/2011 while for cell B 2098 the date shall be 30/06/2018 and at cell B 2100 the date shall be 30/06/2008.
Filter on the blank cells in column B. Then, in the topmost cell (which I'll assume to be B1 but will likely be different), enter a formula similar to the following and fill down
=DATE(C1,6,30)
where the row number in C1 is the same as your first row of data.
You can achieve this with a helper column (any blank column in the same worksheet where you need the dates). In that column enter this formula in the first cell (here in row 2) and copy down.
=IF(ISBLANK(B2),DATE(C2,6,30),B2)
Then copy the Values from the helper column to the date column and delete the helper.
Below is a small macro that is doing the same job. It needs no helper column and over-writes your existing blanks. Before you run it make sure to check the values of the 2 constants at the top and the name of the worksheet (especially the latter!) against your requirements.
Sub WriteStandardDate()
'293
Const FirstDataRow As Long = 2 'change to suit
Const DateClm As Long = 2 'change to suit
' year column must be adjacent to DateClm
Dim R As Long
Dim Arr As Variant
Dim Rng As Range
With Worksheets("Sheet1") ' change name as required
Set Rng = .Range(.Cells(FirstDataRow, DateClm), _
.Cells(.Rows.Count, DateClm).End(xlUp)) _
.Resize(, 2)
Arr = Rng.Value
For R = 1 To UBound(Arr)
If IsEmpty(Arr(R, 1)) Then
Arr(R, 1) = DateSerial(Arr(R, 2), 6, 30)
End If
Next R
Rng.Value = Arr
End With
End Sub
Update: I used the formula suggested by Variatus: =IF(ISBLANK(B2),DATE(C2,6,30),B2) and worked fine through a helper column. There was no need to copy / paste the new dates into the Dates column. I just used the helper column as the new Dates column since full dates from the original column were not changed and got inserted in the helper column thanks to the IFBLANK portion of the formula. Thanks.

Auto Number Cells (1,2,3,etc) if the adjacent cell is not blank

I am new to VBA Excel and wondering if you can help.
I have tried typing something similar to this =IF(C10,(ROW(A10)-ROW(A$9)),"") and it works. However, the downside to this is you have to enter this formula into every cell that you want to auto populate. I am trying to find a macro code in Excel so that cells will auto number 1,2,3,etc. whenever the adjacent column contains data?
For example, when a user enters data into B1, A1 will automatically be populated to 1. Then when users enters data into B2, A2 will automatically be populated to 2 and so on. Then when users delete data from B column, adjacent column in A will not contain a number.
Enter this into A1 based on what you said:
=if(isblank(B1),"",row())
If you want this in VBA you can use the following, but this assumes your data starts at Row 1. If it you want the numbering at Row 2 to start as 1, just add "-1" after the RngA part.
Sub Serial()
Dim RngA As Long, LastRow as Long 'Declares the variables used
LastRow = ActiveSheet.Cells(Cells.Rows.Count, "B").End(xlUp).Row 'This finds the last row in Column B where the loop will end.
For RngA = 1 To LastRow ' This loops from the first row to the last cell filled in column B
If Cells(RngA, 2) <> "" Then ' If Column B is blank, skip this row
Cells(RngA, 1).Value = RngA 'Column B was not blank, so put the row number in column A
End If
Next
End Sub

How to copy data from worksheet to new worksheet a variable number of times?

I have a column of headers in Sheet(2), Column A through Row 108. Columns B through J have data related to those headers. I need to copy Column A with Column B into Sheet(3) "X" times based on an integer in a cell in Sheet(1) into Columns B and C of Sheet(3). This needs to be repeated for each Column B-J making sure to take the header Column A, and paste it "X" times based on an integer in a cell in Sheet(1) into Column B and C of Sheet(3). I need the copy and paste to begin to repeat in Sheet(3) in the row following the last paste of data.
An additional requirement is for each time the two columns of data [meaning Column A and one of the Columns B-J of Sheet(2)] is pasted into Sheet(3), a week of the year is tagged in Column A of Sheet(3) for each row of the data pasted based on a start date in Sheet(1). The week of the year should continue to move away from the start date by one week with every paste of Sheet(2) data into Sheet(3).
Also if the VBA is run again it needs to override the data pasted not continue down the column.
I think I understand your problem. This code takes the number of times to copy/iterate from cell A1 of Sheet1, then it copies data from Sheet2 (Cell B2 to the end of the data set) and pastes it multiple times into Sheet 3:
'assumes parameter is in cell A1
num_iterations = ThisWorkbook.Worksheets("Sheet1").Range("A1")
data_last_row = Worksheets("Sheet2").Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
'Copy records from data tab, assuming data starts in B2
Worksheets("Sheet2").Range("B2:B" & data_last_row).Copy
For i = 1 To num_iterations
output_last_row = Worksheets("Sheet3").Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
Worksheets("Sheet3").Cells(output_last_row + 1, 1).PasteSpecial
Next i

Excel VBA filtering a column and then copying each item in the area collection to a matching row

I have the following Excel Table:
Create Date Last Active Date Age
4/12/2017 5:54 4/17/2020 8:54 5 Days
4/19/2017 7:43 #N/A
4/12/2017 20:43 #N/A
4/1/2017 23:20 4/3/2017 6:54 10 Days
4/15/2017 22:20 #N/A
What I want to do is to filter the Age Column by #N/A, and then copy each Last Active Date value to the same row in Create Date. Seems easy enough, but I keep running into issues. I am using the SpecialCells(xlCellTypeVisible) property to then do a for each on each Area in there(non-contiguous rows), but when I go to copy the rows, it either copies the rows starting at Row 1 of the Create Date column, meaning the values get all out of whack OR it throws an error saying the ranges don't match. Here is the code I have so far that I pulled from another page that talked about how to do this, but it doesn't seem to work for me.
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8, Criteria1:= _
"#N/A"
Dim lngrow As Long
Dim FinalDest As Range
Dim Rng As Range
lngrow = Sheets("Sheet1").UsedRange.Rows.Count
Set FinalDest = Sheets("Sheet1").Range("C2:C" & lngrow)
Range("F2:F" & lngrow).Select
For Each Rng In Cells.SpecialCells(xlCellTypeVisible).Areas
Set FinalDest = FinalDest.Offset(Rng.Rows.Count)
Rng.Copy Destination:=FinalDest
Next Rng
Application.CutCopyMode = False
How can I accomplish this? I want to filter it by #N/A, and then for each filtered row that remains, copy the value in Last Active Date to Create Date, (which will always be blank for these rows) and make sure they get copied to the proper rows, ie if Row 3 is the first filtered row, the value gets copied to rows 3 instead of row 2.
You don't need to filter your table. You have two feasible solutions:
Solution 1 (without VBA):
Assuming that Create Date is at A1:
-Insert a new column between Create Date and Last Active Date.
-Insert this function to B2--> =IF(A2="",IFNA(D2,C2),A2)
-Copy paste B2 till end of your column
-Copy all B column and paste them as values.
-Delete Column A
Solution 2 (with VBA):
You can loop through each row and check if the Age cell is #N/A, if true(#N/A), then you do the copy/paste from Last Active Date cell to Create Date Cell.
Solution 1 is much faster and easy. If you are interested in VBA solution let us know.
Edit: You can always change the following code for different conditions but since you said --> Create date value (which will always be blank for these rows):
Dim i As Long
For i = 2 To Range("C1").End(xlDown).Row
If IsEmpty(Cells(i, 1).Value) Then
Cells(i, 1) = Cells(i, 2)
End If
Next i

excel vba remove duplicates

I have the below table :
Name, Total, Email Address Date
Test1,12,test1#hotmail.com 12/12/2012
Test2,12,test1#hotmail.com 12/05/2015
Test2,12,test1#hotmail.com 12/05/2015
Test3,12,test1#hotmail.com 12/07/2016
I want to match on Name, Email Address and Date. If an existing record is found then I want to merge them and add the totals together. e.g.
Test2,12,test1#hotmail.com 12/05/2015
Test2,12,test1#hotmail.com 12/05/2015
would become
Test2,24,test1#hotmail.com 12/05/2015
What options do I have?. If i iterate sequentially and check for every one it would take a substantial amount of time.. (was thinking to use range check and check current row against all, if found then delete and add a new row).Would appreciate some examples.
You can add a 5th column and CONCAT columns Name,Email,Date, with commas between them. E.g. Test1,test1#hotmail.com,12/12/2012.
Export this column in another sheet and apply Remove duplicates so you have unique data.
Now apply a SUMIF according to this column and sum data from Total as:
SUMIF(range:"your tabel",criteria: the 5th column, sum range: column Total)
Assuming:
The list is sorted by ColA
Your table header is in Row3
There is only 1 duplicate
If ColA is the same, so are C and D
Dim i As Integer
Dim cellCount As Integer
cellCount = Application.CountA(Range("A:A")) - 1
For i = 4 To cellCount
If (Cells(i, "A").Value = Cells(i + 1, "A").Value) Then
Cells(i, "B").Value = Cells(i, "B").Value + Cells(i + 1, "B").Value
End If
Next i
ActiveSheet.Range("A:D").RemoveDuplicates Columns:=1, Header:=xlYes
why not simply have column E sum the values like this:
=SUMIFS(B:B,A:A,"="&A2,C:C,"="&C2,D:D,"="&D2)
and when you finish copy as values and remove duplicates using the function on the ribbon? this way you can copy at the end column E to column B and be done with it in a couple of minutes
edited: clarity and steps
those are the steps needed
place the following formula in cell E1, and pull it down until it's in all of column E until the end of the data
=SUMIFS(B:B,A:A,"="&A2,C:C,"="&C2,D:D,"="&D2)
copy column E into column E - as values
use excel's remove duplicates function
copy column E to column B
and that's the full steps needed to accomplish your task.

Resources