Find duplicates in excel workbook - excel

I have a excel workbook that i want to find all duplicate rows across all the sheets and highlight them. Figured out how todo it in one sheet. Anyone that can help me out?

it's only a starting point...
For b = 1 To Sheets.Count - 1
For e = 1 To 9000
yy = Sheets(b).Range("A" & e).Value
If yy = "" Then Exit For
For k = b + 1 To Sheets.Count
For i = 1 To 9000
If Sheets(k).Range("A" & i).Value = yy Then
Sheets(k).Range("A" & i).Interior.Color = 65500
Sheets(b).Range("A" & e).Interior.Color = 1265500
End If
If Sheets(k).Range("A" & i).Value = "" Then Exit For
Next
Next
Next
Next
This function check between all the sheets, only the column "A" if there are no blanks. Set different color of the first occurrance and the others... If you have a big XLS or the searching criteria are more deeply (check all the cells of a sheets with AND OR NOT ...) the prg can use a lot of time...

Related

Copy values based on match criteria

Data Sheet
I have two workbooks with the same content. I am copying and pasting the amount values from one workbook sheet to another when the project number and division is the same. The amount has to be pasted in the row where there is a match. The issue I am facing is all the amounts are getting copied but not pasted near the respective match.
The code I have used is as follows:
ws1PRNum = "E" 'Project Number
ws1Div = "I" 'Division
ws2PRNum = "E" 'Project Number
ws2Div = "I" 'Division
'Setting first and last row for the columns in both sheets
ws1PRRow = 5 'The row we want to start processing first
ws1EndRow = wsSrc.UsedRange.Rows(wsSrc.UsedRange.Rows.count).Row
ws2PRRow = 5 'The row we want to start search first
ws2EndRow = wsDest.UsedRange.Rows(wsDest.UsedRange.Rows.count).Row
For i = ws1PRRow To ws1EndRow 'first and last row
searchKey = wsSrc.Range(ws1PRNum & i) & wsSrc.Range(ws1Div & i) 'PR line and number is Master Backlog
'if we have a non blank search term then iterate through possible matches
If (searchKey <> "") Then
For j = ws2PRRow To ws2EndRow 'first and last row
foundKey = wsDest.Range(ws2PRNum & j) & wsDest.Range(ws2Div & j) 'PR line and number in PR Report
'Copy result if there is a match between PR number and line in both sheets
If (searchKey = foundKey) Then
'Copying data where the rows match
wsDest.Range("AJ5", "AU1200").Value = wsSrc.Range("AJ5", "AU1200").Value
wsDest.Range("BB5", "BM1200").Value = wsSrc.Range("BB5", "BM1200").Value
wsDest.Range("BT5", "BU1200").Value = wsSrc.Range("BT5", "BU1200").Value
Exit For
End If
Next
End If
Next
This is the area that is causing an issue. As seen in the picture the amounts are pasted even in rows where the division and project number are empty. Any answer for the same would be highly appreciated as I am not well versed with VBA.
You can do this:
wsDest.Range("AJ" & j, "AU" & j).Value = wsSrc.Range("AJ" & i, "AU" & i).Value
'etc...
or with a bit less concatenation:
wsDest.Rows(j).Range("AJ1:AU1").Value = wsSrc.Rows(i).Range("AJ1:AU1").Value

Loop through listbox rows if column is not empty copy paste to empty row in sheet

I'm trying to loop through listbox(attached picture below), and if the column(3) has value, then copy & paste row to empty row in excel sheet for later use.
I wanted to copy the row using column value to put it in sheet, but it just copys last row value, and repeats.
Could you please point out what I did wrong in the below code?
TIA
s = 22
For i = 0 To Me.AnBox.ListCount - 1
If Me.AnBox.Column(3) <> "" Then
Sheets("SparePartsRequestForm").Range("A" & s).Value = Me.AnBox.Column(2)
Sheets("SparePartsRequestForm").Range("C" & s).Value = Me.AnBox.Column(1)
Sheets("SparePartsRequestForm").Range("D" & s).Value = Me.AnBox.Column(3)
s = s + 1
End If
Next i
Userform
Part of excel sheet
Few errors:
The index of your ListBox.Column is zero based, so you not looking at the third but second Index.
You are accessing the values wrongly, trying to read a full column so it seems. The correct syntax is expression.Column(pvargColumn, pvargIndex) so you are missing a parameter. Check the documentation remarks to see the difference between using the second parameter or not.
Make use of the iteration and the more common List property to access each row individually.
So therefor your code could look like:
s = 22
For i = 0 To Me.AnBox.ListCount - 1
If Me.AnBox.List(i, 2) <> "" Then
Sheets("SparePartsRequestForm").Range("A" & s).Value = Me.AnBox.List(i, 1)
Sheets("SparePartsRequestForm").Range("C" & s).Value = Me.AnBox.List(i, 0)
Sheets("SparePartsRequestForm").Range("D" & s).Value = Me.AnBox.List(i, 2)
s = s + 1
End If
Next i
It is possible through the Column property too though:
s = 22
For i = 0 To Me.AnBox.ListCount - 1
If Me.AnBox.Column(2, i) <> "" Then
Sheets("SparePartsRequestForm").Range("A" & s).Value = Me.AnBox.Column(1, i)
Sheets("SparePartsRequestForm").Range("C" & s).Value = Me.AnBox.Column(0, i)
Sheets("SparePartsRequestForm").Range("D" & s).Value = Me.AnBox.Column(2, i)
s = s + 1
End If
Next i
Note: If your intention is to paste at the next available row, there are great ways to return the last used row of a range. See here for example

How to count and copy entries with Excel VBA

currently I am working with an Excel report that has 135.000 rows. There are assets listed in it and I want to count each asset and write it into another worksheet.
I have tried to write a VBA script, which you can find below. It just copies one entry but does not iterate over each row of the worksheet.
Sub assetVulnerabilityCount()
With Sheets("tblExport")
assetCount = 1
rowMax = Sheets("tblExport").Cells(.Rows.Count, "F").End(xlUp).row
currentAsset = Sheets("tblExport").Range("B" & row).Value
For row = 2 To rowMax
If currentAsset = Sheets("tblExport").Range("B" & row).Value Then
Sheets("tblTarget").Range("B" & assetCount).Value = Sheets("tblTarget").Range("B" & assetCount).Value + 1
Sheets("tblTarget").Range("A" & assetCount).Value = currentAsset
Else:
currentAsset = Sheets("tblExport").Range("B" & row).Value
assetCount = assetCount + 1
End If
Next Zeile
End With
End Sub
Ideally, it would look like this:
Worksheet1:
Asset Names:
Laptop1337
Laptop1337
Laptop1337
PC420
PC420
Worksheet2:
Asset Name: Amount:
Laptop1337 3
PC420 2
Worksheet1 is what I have and Worksheet2 is what I need.
If we assume rowMax has the number of rows you need to iterate then
For row = 2 To rowMax
Should loop the rows if
Next Zeile
Is replaced with
Next row
Since it's row that is the variable in the loop.

Replacing strings in a column sequentially

I am naive to macros. I have data in column A with 500 rows of data. Note some rows are blank in between. The data are image files names like (X0011#00.jpg). I need to rename them sequentially with user input. The user input must be only 400500. The new name must be like (400500_Image-1.jpg). The _Image-1, _Image-2 and so on must be generated automatically in sequence omitting blank rows.
See below how my data is displayed in column A and how I want it in column B.
I appreciate if anyone can provide macro for this.
Col A Col B
X0011#00.jpg 400500_Image-1.jpg
X0021#00.jpg 400500_Image-2.jpg
X0041#00.jpg 400500_Image-3.jpg
X0071#00.jpg 400500_Image-4.jpg
X0051#00.jpg 400500_Image-5.jpg
X0031#00.jpg 400500_Image-6.jpg
X0061#00.jpg 400500_Image-7.jpg
X0091#00.jpg 400500_Image-8.jpg
Thanks
Sub naming()
RowsToProcess = Range("A" & Rows.Count).End(xlUp).Row
j = 1
userInput = InputBox("Give me text")
For i = 1 To RowsToProcess
If Not Cells(i, 1).Value = "" Then
Cells(i, 2).Value = userInput & "_Image-" & j & ".jpg"
j = j + 1
End If
Next
End Sub
This macro creates column B as desired

for each loop in excel macros vba jumping alternate rows

I have the below code in vba. The rows in sheet5 from columns a5 to a20 are:
a5=Sweden
a6=Spain
a7=Russia
a8=Italy
a9=Germany
a10=Finland
a11=Norway
a12=Switzerland
a13=France
a14=Belgium
Set fillcolumnrange = Sheet5.Range("A5:A20")
i = 1
For Each Row In fillcolumnrange.Rows
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Row(i)
End If
i = i + 1
Next Row
But this code is prompting only alternate values ie.
Sweden
Russia
Germany
Norway
France
Can anyone please help me out find the bug in the code
You were looping through the rows in your range and also advancing the variable i within your loop.
You can reference each variable that you are looping through.
Try this
Set fillcolumnrange = Sheet1.Range("A5:A20")
For Each cell In fillcolumnrange.Cells
If Not cell = "" Then
MsgBox cell
End If
Next cell
You've got a mixture of different types of loop.
Either do what Rick says.
Or use i:
Set fillcolumnrange = Sheet5.Range("A5:A20")
For i = 1 To fillcolumnrange.Rows.Count
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Sheet5.Cells(i + 4, 1)
End If
Next i
Or maybe a do-Loop
Set fillcolumnrange = Sheet5.Range("A5:A20")
i = 1
do until i = fillcolumnrange.Rows.Count + 4
If Not Sheet5.Range("A" & i + 4) = "" Then
MsgBox Sheet5.Cells(i + 4, 1)
End If
i=i+1
Loop
(EDIT now tested and seem to run ok)
Building on Rick's answer, here's the short version of the For Each loop:
For Each cell in fillcolumnrange.Cells
If len(cell) <> 0 Then MsgBox cell
Next

Resources