Error - Duplicates in excel - vba

I am trying to remove duplicates in excel and have been successful in doing that. I have certain gaps in my spreadsheet after running the code is where I need help. Below is the entire explanation:
1) I copy records from two different files in to a separate file and have 17 columns (A to R).
2) I look for duplicates and remove.
3) 3 Blank rows appear between the last record and second last record after removal of duplicates.(Picture attached)
With y.Sheets("RL Holdings").Activate
Set rng = Range("A2", Range("R2").End(xlDown))
rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
End With

I only see up to column J in your example. I am guessing cell R18 is empty, so that .End will not select the last row, but Q18 has something in it.

Related

Why am I getting an object-defined error while adjust which rows I want to clear?

I have a specific template that starts it's printed array data in cell B9, but I just want rows 9-rows.count to be cleared of it's contents (not deleted because I have drop-down menus in there).
Rows(8).Offset(1, 0).Resize(Rows.Count - 1).EntireRow.ClearContents
doesn't seem to work, but it did work when I had Rows(1).Offset(1,0) etc... in there. why is it now not wanting to work when I adjusted the row number to start on?
Rows.Count gives the maximum numbers of rows in a worksheet (usually 1'048'576).
Splitting your command Rows(8).Offset(1, 0).Resize(Rows.Count - 1):
Rows(8) gives you the row 8 ($8:$8)
.Offset(1, 0) gives you the next row ($9:$9)
.Resize(Rows.Count - 1) asks the range that starts at row 9 to be resized so that it is 1'048'576 rows "high". However, that would mean row 9 until row 1'048'585, and that would be behind the maximum number of rows.
What you probably meant is to delete until the end of your data. Is is not an easy task to figure out the end of the data, usually it is done with a construct like
rowCount = Cells(Rows.Count, 2).End(xlUp).Row. This will (virtually) jump to cell B1048576, simulate the key "Ctrl+Up" and (again virtually) jumps to the last row holding data in that column. Change the 2 into any other column number (A would be 1...) if you want to check that column.
Try the following code:
Dim rowCount As Long
With ActiveSheet
rowCount = .Cells(.Rows.Count, 2).End(xlUp).Row
.Rows(8).Offset(1, 0).Resize(rowCount - 1).EntireRow.ClearContents
End With

Autofill dynamic range based on another worksheet

I've got 2 sheets in a workbook with different sets of data. Sheet 1 has a set of data not formatted as a table. The header row for this data is on Row 4. When I try doing a count using Range("A" & Rows.Count).End(xlUp).Row, I get the last row of that entire sheet, not the count of how many rows of data there are from my starting point.
Sheet 2 has its header row in Row 1. So when I try to use the same count of rows I mentioned above to AutoFill, I always get 3 extra rows because the Sheet 1 count is just looking at what the last row of data is.
I don't want to have to shift things around in either sheet. I just want to be able to autofill based on the same number of rows as there are in Sheet 1, beginning the count at A5 and going down to the last row of data. Is there a different count formula to start at a specific cell and only count the rows below it, and then telling the other sheet to only AutoFill based on that number that I'm missing?
You may be over thinking this. Just discount the row count by 3
Range("A" & Rows.Count).End(xlUp).Row - 3
I would qualify the objects & properties (Range & Row) as well for good measure.
could you use either:
set rng = Range("A1:A1000").SpecialCells(xlCellTypeConstants, 2)
or
set rng = Range("A5").CurrentRegion.Resize(,1)
assuming no blanks in the data range

Excel VBA - Extract Multiple Values from a Single Cell and Associate Values in Source Cell's Row

To preface this, I have very little experience in Excel VBA, but have used some VBA in Access.
I have a file which may contain multiple values in a single cell that need to be extracted out onto individual rows, and then have the data in multiple columns from the source row re-associated with the extracted values.
The multiple values in the single cell that need to be extracted are always in a uniform format. The cell may contain any number of sets of (), but the value I need to extract is always between the 2nd : and the closing ). This is the 'Identifier'.
For example:
(00050008009:STC:363711188)(00040022506:NYC:652263975)
Would need to extract these values onto individual rows:
363711188
652263975
All remaining values from the Source Row the value was extracted from then need to be re-associated with the value.
For example, my file may look like this:
Original File Format
I then need the file to appear as follows, on a new tab:
New File Format
I believe that a module making use of a loop, or multiple loops, is likely what is needed, but I have no idea of how to go about doing this in Excel. I'm open to all solutions. Any help is greatly appreciated! Thank you!
Without writing it for you, here are some pointers to get you started.
You'll need to loop through each cell in the column that contains the information you're looking for. For this, look into Worksheet.Range.
As you go through each cell, you'll need to examine the data that is actually entered into that cell. Using the Worksheet.Range.Value you can extract the contents of the cell.
Use excels string functions to parse the cell value into the values your looking for. Ex: InStr, InStrRev, etc... See this link for your options and usage for each function.
Finally you'll need to insert a row for each value that you find. Lookup Worksheet.Rows.Insert.
This should be the basic framework for what you need to do.
you may want to start with this code:
Option Explicit
Sub main()
Dim myArr As Variant
Dim cell As Range
Dim iRow As Long, nArr As Long
With Worksheets("batches").Range("A1").CurrentRegion '<--| change "batches" with your actual sheet name
For iRow = .Rows.Count To 2 Step -1 '<--|loop through data rows backwards, not to process rows multiple times
Set cell = .Cells(iRow, 3) '<--| 3rd column of current row is being processed
cell = Mid(cell, 2, Len(cell) - 2) '<--|get the cell value between first and last bracket
myArr = Split(cell, ")(") '<--|parse the resulting string with ")(" as delimiter and obtain and array
nArr = UBound(myArr) '<--| calculate the array size
If nArr > 0 Then '<--| if more than one element in array...
With .Rows(iRow) '<--|... then refer to entire current row
.Offset(1).Resize(nArr).Insert '<--| ...insert n-1 rows...
.Resize(nArr + 1).Value = .Value '<--|...duplicate current row into newly inserted ones
End With
cell.Resize(nArr + 1).Value = Application.Transpose(myArr) '<--|fill 3rd column of current and newly inserted rows with array elements
End If
Next iRow
For iRow = 2 To .Rows.Count '<--|loop through data rows
With .Cells(iRow, 3) '<--| 3rd column of current is being processed
.Value = Right(.Value, Len(.Value) - InStrRev(.Value, ":")) '<--| "finish" it
End With
Next iRow
End With
End Sub
As per your example, it assumes that your data start from cell "A1" and there's no blank row or column between it and the bottom-right cell of your data

Adjust criteria to depend on multiple values

I have been working on a code to copy the data from one specific range(always the same) and paste in another spreadsheet always in the row below. So basically, it starts pasting on row 11, but if I run again it will paste on the row 12 and there it goes.. The code has been working fine, but there is only one problem. It identifies the next empty row(to paste) based on the value of the column AP, but i want it to identify based on the values of all the columns between AP:BA. Thus, if there is any value on those cells, it should copy on the row below, not only if there is a value on AP. Does someone know how to change my code in order to solve this problem? Thank You very much
Sub Copy_Shanghai()
Dim count As Integer
count = 11
Do While Worksheets("Time Evolution").Range("AP" & count).Value <> ""
'<>"" means "is not empty", as long as this happens we go down looking for empty cell
count = count + 1
Loop
'Now count is row with first empty cell outside of top 10 rows in column C
Worksheets("Fill").Range("E5:P5").Copy
Worksheets("Time Evolution").Range("AP" & count).PasteSpecial xlPasteValues
End Sub

Compare worksheets and insert new rows

I currently have two sheets with six columns of data, both in the same format, except that Sheet 1 has historic data and sheet 2 has newer data with some additional rows. Both sheets are sorted in order of the contents in the 2nd column followed by the 4th column.
I want to prepare a macro that compares both sheets and looks down the 2nd and 4th columns to identify the new rows in Sheet 2 that are not in Sheet 1 and color highlight these rows in Sheet 2. In addition, I would like the new rows from sheet 2 to be inserted into Sheet 1 in the correct order.
For Example
The reason for doing all this as opposed to just copying the entire contents of Sheet 2 into Sheet 1 is because sheet 1 has a number of formulas beyond the 6 columns which reference certain blocks of cells and it is require that these references be preserved. I currently have to manually insert each new row and given the amount of data being processed, this takes quite some time. I have tried adapting other macros that I found across the internet to perform this task but they don’t quite work.
Step #1: identify rows that are in sheet2 and not in sheet1
Create a new column E in both sheets with this formula:
=B2&D2
(starting from row 2 and auto fill it to the entire column)
in sheet2 create column F with this formula
=ISERR(VLOOKUP(Sheet2!E2,Sheet1!E:E,1,FALSE))
Now column F would be TRUE only for rows that aren't in sheet1.
Next you'll need to add conditional formatting for F=TRUE
Step #2: Copy the missing data
Filter rows from sheet2 with F=TRUE
Copy them to the end of sheet1
Sort sheet1
If you copy the data (excluding the header) from Sheet2 underneath the data in Sheet1 and subsequently a) remove duplicates then b) sort on columns B and D, you should achieve the results you are looking for.
Sub collect_and_sort()
With Sheets("sheet1")
Sheets("sheet2").Cells(1, 1).CurrentRegion.Offset(1, 0).Copy _
Destination:=.Cells(Rows.Count, 2).End(xlUp).Offset(1, -1)
With .Cells(1, 1).CurrentRegion
.RemoveDuplicates Columns:=Array(2, 4), Header:=xlYes
.Cells.Sort Key1:=.Columns(2), Order1:=xlAscending, _
Key2:=.Columns(4), Order2:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
End With
End Sub
From the data on two sheets like this (shown on one sheet for space considerations),
    
You will have this after the macro has run.
    
I will admit I am unclear on whether the 3000/b in Sheet1!B11:D11 was a typo or an actual duplicate record. The macro does reproduce your desired results.