Removing cells with duplicate values from Range object - vba

I've a Range object which its cells contains duplicate values.
How can I obtain a Range object with all duplicate values left out?
Although not required, assume that every first unique occurrence should be in the resulting Range object.

Record a macro while issuing the Data, Remove duplicates command as that is precisely what that does.

Related

How to count Blanks in N'th column in a ListObject table

I want to count Empty cells (Blanks) in the fourth column only. I tried the following code.
ActiveSheet.ListObjects(1).ListColumns(4).DataBodyRange.SpecialCells(xlCellTypeBlanks).Count
In my case, I have a table with only one row and 4'th column has non-empty cell. I get the wrong value with my code. The code above returns 3 Blanks instead of zero - 3 blanks could be total number of blanks in table.
I do not know the specifics behind this bug but it seems closely related to a behavior that used to show up when you tried to reference the visible cells below the header in an AutoFilter range when the filtered range showed no visible cells. In that case, it returned all non-visible rows. While not completely the same, this miscount seems closely related. To the best of my knowledge, that bug has been corrected in Office service packs.
Use the Excel Application object to call a worksheet's COUNTBLANK function to achieve the correct result.
With Worksheets("Sheet1")
Debug.Print Application.CountBlank(.ListObjects(1).ListColumns(4).DataBodyRange)
End With

Range object containing all cells of a worksheet

I am looking for a command that creates a Range Object containing all cells of a given worksheet.
Sheets(1).Range("A1:AAZ1000000") would be a safe bet, but you never know how many rows or columns you might encounter so something like
Sheets(1).Range("All") would be better.
Sheets(1).Cells returns a Range object and when called with no parameters returns a range encompassing all the cells in the sheet.
Sheets(1).EntireRow.EntireColumn is one way

Worksheet Range Reference - Expands based on data added/removed

Trying to make an array change based on the length of the data added. The data has to be deleted except for the top row starting at A:4 and then pulled via a database that creates rows drilling down to the bottom of the node(s). This can vary from A10:Axxxxx depending on the data in that node.
=AVERAGE(IF((MOD(ROW(Repurchase!E4:E45)+4,6)=0)*(Repurchase!E4:E45)<>0,(MOD(ROW(Repurchase!E4:E45)+4,6)=0)*(Repurchase!E4:E45)))
What I have so far and it works. But of course once the data is deleted and re-pulled
Repurchase!#REF!
shows up and I have to manually add back in A4:Axxxx.
Any help would be appreciated. Can use VBA if needed but was looking for a non-macro worksheet solution.
Replace all references of Repurchase!E4:E45 with,
Repurchase!E4:INDEX(Repurchase!E:E, MATCH(1e99, Repurchase!E:E))
This will create a range from E4 down to the last number in column E. You can delete rows without consequence but the range will dynamically reshape to the new proportions. There can be blank or text within the range. It will always be from E4 to the last number in the column.
If someone suggests the OFFSET function, tell them you would prefer to avoid volatile functions.
If the rows could potentially affect E4 as well, then use the INDEX function to reference both sides of the colon in the range reference.
INDEX(Repurchase!E:E, 4):INDEX(Repurchase!E:E, MATCH(1e99, Repurchase!E:E))

Accessing Certain Column in a Range

I am trying to access column 2 in a specific range, and then counting the cells with constants in them. Right now what I have is accessing the range, but not a specific column in the range, but rather the entire range... thus counting too many constants.
X = Range("MAIN_LIST").Cells.SpecialCells(xlCellTypeConstants).Count
Is there a way for me to access a specific column in this range? I tried to do this somehow with:
X = Range("MAIN_LIST").Column(2).Cells.SpecialCells(xlCellTypeConstants).Count
but am getting a syntax error. Anyone know the specific syntax to do this?
Thanks.
Try just:
Range("MAIN_LIST").Columns(2).SpecialCells(xlCellTypeConstants).Count
The method is Columns, not Column.
http://msdn.microsoft.com/en-us/library/office/ff837125(v=office.14).aspx
Also, while it's not an error: Using the method Cells is redundant/unnecessary, the Range object itself already is comprised of its Cells, and likewise any particular column in that range is also comprised of its Cells :)

How to loop over a multi-row/column range per column using Excel 2007 VBA?

I'm looping to loop over a range with multiple rows and columns using Excel 2007 VBA.
I'm specifically trying to loop over a user selected range, and perform a calculation on each column in the range, and output the calculation two rows below each column.
You can retrieve the currently selected range by using
Application.Selection.Address
This will give you a range value (the Selection property returns a Range object) that will look something like "$B$4:$J$20".
Given you now have a range to work with, you can iterate across each column using something like:
For Each col In userSelectedRange.Columns
...
Next
The Columns property again returns a Range object that you can either iterate over further or perform other calculations on (your exact needs aren't too clear from your question).
To post the calculated result two rows above each column (e.g. a subtotal or similar), try using the Offset function:
Cells.Offset(-2, 0)
If you're able to provide more specifics around the sort of calculation you want, I may be able to add more detail into how you achieve it.