Excel VBA - Get rid of duplicate values in a particular column - excel

I have a worksheet with many rows of IDs.
I would like to know the best way to write a VBA procedure that will look at the range of values in one column, and replace the entire range with only the unique values which appear in that range. So a column of 1000 IDs might reduce down to a column of 150 unique IDs. I would not like this procedure to affect the data in other columns in the worksheet.
So, say the initial column A was:
*IDs*
ID12
ID12
ID34
ID56
ID78
ID78
ID78
I would like it to replace the column with a new column A:
*IDs*
ID12
ID34
ID56
ID78
Thank you kindly.
Note: I know how to do this manually a few different ways, but I would like to cycle through and do this procedure for every non empty column on a sheet, and the columns are of varying length.

To achieve what you want please do the following:
Select entire column you want to remove dupes of.
Go Ribbon Data > Remove Duplicates.
Set My data has headers (according your input).
You're done. If you want these steps in VBA - turn on macro-recorder before the start (bottom left corner of Excel window).

Related

In excel, how can I automate a sheet to be filled based on conditions of another sheet?

Is there a way in excel to copy the contents of one column to another sheet based on particular values in another column.
I have data which looks like this:
Sheet1:
Sheet2:
I would like to copy the column A of sheet 1 directly onto column A of sheet 2, but only if the value in column B is Y.
I have tried used the match function but am not sure how the best way to do this would be. Is there a way for me to achieve this?
My desired output is:
As discussed in the comments, #Waldorf99 was looking to have a second worksheet that would automatically show a filtered list from the first sheet. I can think of a few ways to do this (array formulas or pivot tables come to mind). The problem with mixing dynamic columns with static values is that the static values would become desynced from the dynamic ones.
In the original example, rows may have a blank value in the condition column in sheet one, and then may be assigned a Y or N at a later date. If a Y is assigned to a row in the middle of other rows, the filtered sheet would shift the existing rows down to make room. The static values would stay where they were, and would become desynced. To demonstrate:
If the above image is the original state of sheet 1 and 2...
...adding a y next to x.1.c would result in sheet two shifting columns A and B of row 2 down, but leaves columns C and D behind (as they are static, and not tied into the first two columns in any way).
One thing that may work as you are wanting are filters. You would only have one sheet, with all of the data manually entered. Then you can add filters, and change them to hide rows temporarily when needed.
To add and use filters:
Start with your data all on one sheet...
Highlight your data...
On the Home tab, select "Format as Table" and choose any style...
This turns your data into a table. You can filter by clicking the drop down in a given columns header row, then deselecting the values you want to hide.
The results are a table that only shows the rows with a 'Y'.
The other rows aren't removed, just hidden. You can always reset or change your filters, to configure which rows are visible.
Note: when working with tables, they will auto expand to account for new rows, so long as you work in the row directly under the table (e.g. don't leave blank rows). You can also manually resize the table at any time by clicking and dragging from the bottom right corner of the table.
There are tons of resources of Excel tables online, and it's a pretty useful tool in Excel.
Hope that achieves what you were looking for.

How to clear a row cell values with macro in a data table

I use a table for work that has about 10 rows but I want to expand it to as high as 200. I use this table to keep track of specific tickets that I need to work on that shows me the status and follow up date. I use formulas in the adjacent cells to give me follow-up dates and specific comments based on the values of the first 3 cells in the row.
This is basically what I am trying to have setup... The first column of the table has a shape/button with a macro attached to it. I use this macro to clear the contents of the next 3 cells to the right of it. I want a button for each row of the data table to use as a simple way to delete entries. I use another macro to sort the table so that there are no blank entries between others in the table.
I use this code to clear the contents and have to edit it for a new row:
Range("B4,C4,D4").Select
Selection.ClearContents
Is there an easier method to accomplish this without having to write a macro for each of the buttons for the rows next to it?

Excel formula for counting the number of incidents of a word in a column?

Is there a quicker way of searching for terms without typing each one into the formula? Like, say I have a column that has a bunch of names of locations and I want to find out how many times each one comes up.
This is the formula for when I type in the locations:
=COUNTIF($F$2:$F$274,"*AD library*")
I just modify the AD library to the next one, say monastery so it would be
=COUNTIF($F$2:$F$274,"*monastery*")
Is there another way of getting the same info without having to type in each one (it's a big sheet with a lot of locations).
Thanks
use PivotTable on just that one column. Put that column in both rows and Values:
Go to the insert tab and insert a pivot table.
Then drag the header (locations?) you want the count of to row labels and any other header to the value field (preferably something with text).
Now the pivot should give you the count of each item.
If you don't have a column with text then choose any other and you need to switch from sum to count in the value field settings.

how to optimize speed excel 2007 (±20,000 rows)

I'm in the process of working with an Excel file that contains two columns (old URL and new URL). But it contains about 20,000 rows.
And I have another file containing about 400 old/new URL that needs to be imported in the big ±20,000 rows file.
I have to do all kinds of processing, like:
- Find all duplicate rows (same two columns more than once...). That functionnality would be in a column and it would be good to run that function each time I add 1 row to check if that URL combination already exists in the file
Note that I already turned the sheet into a table.
2 questions now:
1) should I do some kind of vlookup from the ±20,000 rows sheet and the ±400 rows sheet, or VBA? I don't know what would be the best way to do this (i.e.: if that row from the ±400 rows sheet is not in the ±20,000 rows sheet, add it...). Should I use vlookups or populate arrays in VBA (speed-wise)? If I use vlookup, it is true that it is possible to put the vlookup function in a sheet and refer to it in every row instead of puting a vlookup function directly in every row?
2) How can I optimize the 20,000 rows sheet because now, each time I want to sort or filter, it takes an eternity to redraw and it freeze my PC for that time!
Thanks for you help.
Firstly to ommit the dupes from the 400ish row sheet that need to be added in, use a COUNTIFS formula against the big sheet, then sort by this value and only copy in things where there is < 1 for the value (or error).
Secondly I would probably do the same thing in the big sheet but referencing itself, anything with a value above 1 is a dupe.
Lastly, are there formulas in the 20,000 row sheet? I could set up a 20,000 row sheet with just a "1" in range A1:A20,000 and doing anything on it would be super quick. It all comes down to what data you have in there and what you can do to reduce it's load on the system (ie convert formulas to values if they no longer need to calculated)
Excel 2007 has a built-in feature and VBA you can use for your situation: Range.RemoveDuplicates or Data tab -> Data Tools group -> Remove Duplicates
For example data:
Click the Remove Duplicates button:
And you are done!
The VBA equivalent is:
ActiveSheet.Range("$A$1:$B$10").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
Note the 1 & 2 does not mean Columns A & B. It means the Columns of the selected Range.
If your worksheet only contains 2 columns, you could use UsedRange instead.

How to check if two rows are identical and contain the same data in a list of rows

I have a list of lines in the D column and every line has a few properties in columns I, J, K and so on. I want to check my list if any rows contain the same properties and same line number. I want to delete the identical row and all the information in the columns. I'm new to VB so have no idea where to begin. My Sheet is called "Sheet1" and the column begins at D30 and goes to the right up until column AK30. The row begins at 30 and goes down to 7000. If anyone could help me with this it would much appreciated.
Thanks
My idea is use something very similiar to the used in
this post:
Add a new column where you "Concatenate" all the paremeters to be checked in the row.
Seleect all your data and "Remove Duplicates" that appear based in the column with concatenated results.
concatenate is a formula to string together the input values, where Remove Duplicates is self explanatory and is under Data tab in the Data Tools subtab (in excel 2007).
Regards

Resources