How to Check for Duplicates and Display a Count MsgBox - excel

I have Three worksheets, and essentially I want to select a cell in Column A of Sheet 2 (As the Active Cell) and check if there are any duplicates in Column A of Sheet 3 (The Range for this Sheet should be from A1 to the last row of Data).
If there are any duplicates, I would like a msgbox to display the number of duplicate values if it's greater than 3.
I have added comments explaining my logic in each step, please feel free to simplify my code as well:
Sub Check_Duplicates()
'Declaring variables
Dim Cell As Variant
Dim Source As Range
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Dim rowAC As Long
Dim Counter As Long
'Assigning a worksheet to the decalred variables
Set sh1 = Sheet1
Set sh2 = Sheet2
Set sh3 = Sheet3
'Sets the Long variable as the Active Cell Row in Sheet 2
rowAC = ActiveCell.Row
'Initializing "Source" variable range to last row in Sheet 3
Set Source = sh3.Range("A1", sh3.Range("A1").End(xlDown))
'Looping through each cell in the "Source" variable Range
For Each Cell In Source
'Checking if the "Cell" values in Sheet 3 (in column A to the last row) are equal to the value in the Active Cell in Column A
If Cell.Value = sh2.Range("A" & rowAC).Value Then
'Checking whether the value in "Cell" already exists in the "Source" range
If Application.WorksheetFunction.CountIf(Source, Cell) > 1 Then
'Counts and stores the number of duplicate values from Sheet 3 "Cells" compared to the Active Cell value in Sheet 1 Column A
Counter = Application.WorksheetFunction.CountIf(sh3.Range("Source,Cell"), sh2.Range("A" & rowAC))
'If there are more than 3 duplicates then display a message box
If Counter > 3 Then
'Msgbox displaying the number of duplicate values in Sheet 3
MsgBox "No. of duplicates is:" & Counter
End If
End If
End If
Next
End Sub
Currently, my code gets to the first IF Statement and simply goes to the End IF, so it doesn't execute past this line and simply goes to Next and then End Sub:
If Cell.Value = sh2.Range("A" & rowAC) .Value Then
Cross Referencing:
https://www.mrexcel.com/board/threads/how-to-check-for-duplicates-and-display-a-count-msgbox.1125070/

Here is the final code I am using for anyone using this question as reference for their issues:
Sub Check_Duplicates()
'Declaring variables
Dim Source As Range
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Dim rowAC As Long, Counter As Long
'Assigning a worksheet to the decalred variables
Set sh1 = Sheet1
Set sh2 = Sheet2
Set sh3 = Sheet3
'Sets the Long variable as the Active Cell Row in Sheet 2
rowAC = ActiveCell.Row
'Initializing "Source" variable range to last row in Sheet 3
Set Source = sh3.Range("A1", sh3.Range("A" & Rows.Count).End(xlUp))
'count number of times is in Source range
Counter = Application.WorksheetFunction.CountIf(Source, sh2.Range("A" & rowAC))
'If there are more than 3 duplicates then display a message box
If Counter > 3 Then
'Msgbox displaying the number of duplicate values in Sheet 3
MsgBox "No. of duplicates is: " & Counter
End If
End Sub

Related

Dump data to specific Cell in a different Sheet in the same Workbook

I was looking for a VBA script to write data from a table to specified cell in a worksheet.
For eg: In the image table - column 1 is serial number, column 2 is data to be written, column 3 is sheet to which it should be written and column 4 is the cell in the corresponding sheet.
I am looking for a VBA script to write '1' from row 1 & column 2 to cell "A1" in sheet "A".
Here's code for it:
Sub Dump2Print()
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Dim rng As Range: Set rng = Application.Range("Dump!A1:A" & LastRow)
Dim i As Integer
Set wb = ActiveWorkbook
For i = 1 To rng.Rows.Count
MyValue = rng.Cells(RowIndex:=i, ColumnIndex:="B").Value
MySheet = rng.Cells(RowIndex:=i, ColumnIndex:="C").Value
MyRange = rng.Cells(RowIndex:=i, ColumnIndex:="D").Value
Set ws = wb.Sheets(MySheet)
ws.Activate
ws.Range(MyRange) = MyValue
Next
End Sub
This code will look at all the data in the table in sheet named "Dump" and read values from column "B","C" & "D".

Highlight Non Match From List On Another Worksheet?

I am having trouble with getting the below VBA code to highlight the non-matches in column H on the sheets with the name "Revision". The master matching list is called "Materials" and it is column A there. Every time I run my current code, the whole column in the Revision sheet turns red. How do I get it to only highlight the non-match?
For example, say the "Materials" master sheet has the list Copper, and Steel. On the revision sheet it has Aluminum and Steel. How can the sheet only highlight the cell that says Aluminum in red?
Thanks.
Private Sub MaterialMatch()
Dim iRow As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws2 = Worksheets("Materials")
Dim FoundRow As Variant
Dim lRowSource As Long
For Each ws1 In ActiveWorkbook.Worksheets
If ws1.Name Like "Revision *" Then
lRowSource = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row 'find last row in column A
For iRow = 2 To lRowSource
FoundRow = Application.Match(ws1.Cells(iRow, "H"), ws1.Columns("A"), 0)
If IsError(FoundRow) Then
ws1.Cells(iRow, "H").Interior.ColorIndex = 3
End If
Next iRow
End If
Next ws1
End Sub

Trying to copy cell format from one sheet to another, cell by cell

Sub Start()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("1")
Set ws2 = Sheets("2")
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, "D").End(xlUp).row
For i = 1 To LastRow
ws1.Range("D" & i).copy
ws2.Range("A2").PasteSpecial Paste:=xlPasteFormats
Next i
If ws1.Range("A" & i) = vbNullString Then
Exit Sub
End If
End Sub
I'm trying to make it so this code pastes the interior fill from column D, Into Column A, unless there is already interior fill present.
Any input appreciated.
Edit: I'm trying to copy the format (interior fill) from Sheet 1 and Sheet 2's Column A's (until the last row) and paste it to Sheet Column A. I'm trying to copy over once cell at a time, and ignore if there is already fill present in sheet 3, because my I dont want the format from sheets 1 and 2 to overwrite eachtoher.

copy selected columns in a row to another sheet if a cell meets a condition

(not in a range, not adjacent columns)
(in given order)
I have many rows on Sheet1. I would like to copy some columns of a row (not the entire row and not a range of columns) to Sheet2 (to the first empty row of Sheet2) if a cell satisfies a condition (the cell in the current row and A column has a value of y)
I would like to copy not the entire row from Sheet1 only the row with those columns that are given on Sheet3 (Column A), and the new column number (on Sheet2) is also given on Sheet3 (column B)
It would be simple if my task would be to copy the entire row, or the selected column would be in a range...but i would need to copy those columns that are specialized on Sheet3. I would be grateful for any help. Thanks in advance.
Sheet1 shows an example data sheet. The criteria is if Cells(Rows, 1).Value = "y"
Sheet2 shows the desired result.
Sheet3 shows the selected column number on Sheet1 and the new column number on Sheet2
Whilst this probably should be done using arrays more, here's some basic VBA code that loops the first sheet checking for "y" in the first column. When it finds it, it then loops the column mappings in the third sheet that have been saved into arrays to set the values on the second sheet:
Sub sTranasferData()
On Error GoTo E_Handle
Dim aOld() As Variant
Dim aNew() As Variant
Dim wsIn As Worksheet
Dim wsOut As Worksheet
Dim wsTrack As Worksheet
Dim lngLastRow As Long
Dim lngLoop1 As Long
Dim lngLoop2 As Long
Dim lngRow As Long
Dim lngTrack As Long
Set wsIn = Worksheets("Sheet1")
Set wsOut = Worksheets("Sheet2")
Set wsTrack = Worksheets("Sheet3")
lngLastRow = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row
lngTrack = wsTrack.Cells(wsTrack.Rows.Count, "A").End(xlUp).Row
aOld() = wsTrack.Range("A2:A" & lngTrack).Value
aNew() = wsTrack.Range("B2:B" & lngTrack).Value
lngRow = 1
For lngLoop1 = 2 To lngLastRow
If wsIn.Cells(lngLoop1, 1) = "y" Then
For lngLoop2 = LBound(aOld) To UBound(aOld)
wsOut.Cells(lngRow, aNew(lngLoop2, 1)) = wsIn.Cells(lngLoop1, aOld(lngLoop2, 1))
Next lngLoop2
lngRow = lngRow + 1
End If
Next lngLoop1
sExit:
On Error Resume Next
Set wsIn = Nothing
Set wsOut = Nothing
Set wsTrack = Nothing
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "sTransferData", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards,

loop through rows and columns to meet criteria

I would be very greatful if someone could help me with this issue...
I would like to have a excel macro which would go through first row and first column of the sheet2 to return the value if booth conditions are met in cell b3 in sheet1.
Conditions would be specified on sheet1; cell b1 would contain condition by which rows in sheet2 should be searched and cell b2 would contain condition by which columns in sheet2 should be searched. Result should be copied in cell b3 in sheet1.
Thanks in advance
Addition..............
I have this sub which goes through rows and looks for condition 1 (strDate) but I only managed to do this is column is fixed. I should add one more counter which would go through columns to meet condition 2 (strProduct) but I don
Sub LookUpValuesRCC2()
'
Dim shtData As Worksheet ' Sheet containing data
Dim shtOutput As Worksheet ' Output Sheet
Dim strDate As String ' Date - condition 1
Dim strProduct As String ' Product - condition 2
Dim i As Integer ' Counter in shtData Sheet
Dim j As Integer ' Counter in shtOutput Sheet
'
Set shtData = Worksheets("sheet2")
Set shtOutput = Worksheets("sheet1")
'
' Loop through "Data" Sheet Rows
For i = 1 To 1000
strDate = shtData.Cells(i, 1)
'
' Loop through dates in "Output" Sheet
' if date match then vrite values
For j = 1 To shtOutput.Cells(Rows.Count, 14).End(xlUp).Row
If shtOutput.Cells(j, 14) = strDate Then
shtOutput.Cells(j, 2) = shtData.Cells(i, 18)
End If
Next j
Next i
End Sub
First welcome to SO. Second, it's not 100% clear what your after because your code doesn't exactly match the description of what you want, or doesn't appear to me to do that.
I've written the below code based on what your description says, since the code you have doesn't get you want you want, so I am going to assume it needs modification anyway.
Comment if this doesn't satisfy your requirement.
Sub LookUpValuesRCC2()
Dim shtData As Worksheet ' Sheet containing data
Dim shtOutput As Worksheet ' Output Sheet
Dim strDate As Date ' Date - condition 1
Dim strProduct As String ' Product - condition 2
Dim strResult As String 'result to print
Dim rngFound As range, rngFoundAgain As range
Set shtData = Worksheets("sheet2")
Set shtOutput = Worksheets("sheet1")
strDate = shtOutput.range("B1").Value
strProduct = shtOutput.range("B2").Value
strResult = "Nothing Found"
With shData
'first look down the first column for the date
Set rngFound = .Columns(1).Find(strDate, lookat:=xlWhole)
If Not rngFound Is Nothing Then
'if that is found, look for the product in the row with the date
Set rngFoundAgain = rngFound.EntireRow.Find(strProduct, lookat:=xlWhole)
If Not rngFoundAgain Is Nothing Then strResult = rngFoundAgain.Value
End If
End With
shtData.range("B3") = strResult
End Sub

Resources