Somehow after few days of googleing I didn't find any satisfying answer.
I have to find duplicates in the same column and copy them both (or more) to the new sheet to show where are the issues.
The only way I managed to do that was
For i = 2 To lastCell
If dataArray(i, 3) <> "" Then
For j = i + 1 To lastCell
If dataArray(i, 3) = dataArray(j, 3) Then
results.Range("A" & k & ":" & lastCol & k).Value = checkbook.Range("A" & i & ":" & lastCol & i).Value '
results.Range(commentAddress & k).Value = "Duplicate ID"
k = k + 1
results.Range("A" & k & ":" & lastCol & k).Value = checkbook.Range("A" & j & ":" & lastCol & j).Value
results.Range(commentAddress & k).Value = "Duplicate ID"
k = k + 1
End If
Next j
End If
Next I
But this is taking too long! I found that dictionary could be very helpful but don't really know how to use this - and it only shows the SECOND value (I need both)
So are there any other solutions to find duplicates? I need the fastest one as the file I am working on has 100K+ rows (loop in a loop is killing me)
You could try something similar to this to recreate the column with blanks replacing the duplicate values. Then you can just filter on blank values in the column to find which values or IDs are duplicates. Or write a formula that loops through column and collects addresses of cells that are empty.
=IF(A1="";"";IF(COUNTIF(A1:A100;A1)=1;A1;""))
Replace A1:A100 with the range your data occupy, or the whole column if you prefere.
Related
So Every time I run my code I get the Types Mismatch error and I can't find what to change into what in order to run it properly. Even though this might still not entirely Solve the greater problem which is how to formulate my question into the vba code, but first things first. How to get this line of code without errors:
Range("I" & r & ":" & "I" & B).Value = Range("I" & r & ":" & "I" & B).Value + 1
Where r = the current row the code is checking ( 5 to 44)
and B is the last row the code can check ( 44)
All I want this line to do is to add one to the already existing value of the cell (which is 0 if nothing is done in that row or a formula if conditions are met, this formula will make a value from 1 to 40)
You cannot do this the way you are doing -> you cannot add a number to a range of cells at once. You must add the number to one cell at a time.
Try this:
Dim i As Integer
For i = r To B
Range("I" & i).Value = Range("I" & i).Value + 1
Next
To handle formulas:
Dim i As Integer
For i = r To B
Dim numberToAdd As Integer
numberToAdd = 1
If Range("I" & i).HasFormula Then
Range("I" & i).Value = Range("I" & i).Formula & "+" & Trim(Str(numberToAdd))
Else
Range("I" & i).Value = Range("I" & i).Value + 1
End If
Next
I am trying to use VBA to concatenate everything between two specified rows. What's the best way to go about this?
Basically I want to leave the lines where the third cell is "U" intact, and make the sixth cell of that row the concatenation of the rows below, until we run into another row that contains a "U" in the third cell. Then the process would repeat. The number of rows between the cells containing "U" is varied.
Pic is below
Ok, this should work (haven't tested it though):
Sub My_Amazing_Skills()
Dim l As Long, i As Long
l = 1
i = 1
Do Until i > Range("A1048576").End(xlUp).Row
If Range("C" & l).Value = "U" Then
i = i + 1
Do Until Range("C" & i).Value = "U"
Range("F" & l).Value = Range("F" & l).Value & " " & Range("C" & i).Value
i = i + 1
Loop
Range("F" & l).Value = Trim(Range("F" & l).Value)
End If
l = i
Loop
MsgBox "Bow down to the great Jeremy!", vbInformation, "Your concatenating is done"
End Sub
I presume you know know where to copy this to?
I have a spreadsheet that I need to sum how many values "UDL" there is in column(N:N), but the problem is they should only be counted for every unique number in column (C.C)
So for every number that also has "UDL" in column N they should only be counted once as there can be a lot of duplicates.
Hope you can help :)
Seems like a loop & delimited string would be a quick way to get what you're wanting. Assuming Column A is always filled & has a header in row 1, something like:
Dim u as string
Dim i as long
dim t as long
i = 2
Do While Range("A" & i).value <> ""
If Range("N" & i).value = "UDL" Then
If InStr(1, u, "|" & Range("C" & i).value & "|") = 0 Then
t = t + 1
u = u & "|" & Range("C" & i).value & "|"
End If 'else the value in C is non-unique
End If 'else not UDL so no action needed
i = i + 1
Loop
Range("A" & i + 1).value = t 'Outputs t to column A with a blank row between it & your data
That'll need some adjusting based on what column should control the loop, what row the loop should start on, and where you want the count to go.
I have some datas where there are multiple duplicates in column E and its dept no. in column S. What I want to do is, for any duplicate value in col E if the values of Col S are same, then it should retain the same value in the 1st duplicate and delete other duplicates. If the Col S values are not same, it should have the value as "18" in it. Eg
Col E Col S Ans
1515A 10 Retain no changes
1515AA 12 Retain as 1515AA in Col A and 12 as Col S
1515AA 12 Delete
1515AA 12 Delete
5151B 8 Retain no changes
515BB 5 Take 515BB with 18
515BB 3 Delete
I have nearly 800-1500 line items. Can anyone help me with a macro. It will be very useful for me, instead of manually finding and deleting datas.
You can use something like:
Sub EraseR()
i = 1
While Range("E" & i).Value <> ""
If (Range("E" & i + 1).Value = Range("E" & i).Value) And (Range("S" & i + 1).Value = Range("S" & i).Value) Then
Range(i + 1 & ":" & i + 1).Delete
ElseIf (Range("E" & i + 1).Value = Range("E" & i).Value) And (Range("S" & i + 1).Value <> Range("S" & i).Value) Then
Range(i + 1 & ":" & i + 1).Delete
Range("S" & i).Value = 18
Else
i = i + 1
End If
Wend
End Sub
I have an Excel Macro as follows:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim i&, j&, k&, LR_A&, LR_B&, LR_C&, count&
LR_A = Range("A" & Rows.count).End(xlUp).Row
LR_B = Range("B" & Rows.count).End(xlUp).Row
LR_C = Range("C" & Rows.count).End(xlUp).Row
count = 1
For i = 1 To LR_A
For j = 1 To LR_B
For k = 1 To LR_C
Range("D" & count).Value = Range("A" & i).Value
Range("E" & count).Value = Range("B" & j).Value
Range("F" & count).Value = Range("C" & k).Value
count = count + 1
Next k
Next j
Next i
Application.ScreenUpdating = True
End Sub
As you can see, it is resorting the data in columns A, B, and C into D, E, and F.
What code do I need to add to get it to ignore the column headers in A, B, and C? I have searched around but have been unable to find an answer.
In addition, how would I change it so that it instead of resorting the data into D, E, and F, it is resorting it into A, B, and C on another worksheet?
This is my first macro, so they are pretty basic questions.
Thanks in advance
Answer to your question
To avoid copying the headers, you should begin your loops at index 2.
To copy values to another Worksheet, you need to call it in your Range.
For i = 2 To LR_A
For j = 2 To LR_B
For k = 2 To LR_C
Sheets("My other ws").Range("D" & count).Value = Range("A" & i).Value
Sheets("My other ws").Range("E" & count).Value = Range("B" & j).Value
Sheets("My other ws").Range("F" & count).Value = Range("C" & k).Value
count = count + 1
Next k
Next j
Next i
Maybe a better way to handle this case
Looping over range using index can be very slow.
I advise you to have a look at this link: http://www.ozgrid.com/VBA/VBALoops.htm where the author explained faster ways to loop over ranges.
Some tips
Have a look at Excel SpecialCells as described here.
For instance, in your case, you could try to use ActiveSheet.UsedRange instead of finding the last used row.