Accessing Certain Column in a Range - vba

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 :)

Related

Removing cells with duplicate values from Range object

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.

Need VBA script to extend Excel table every week

The problem is as follows: I have several tables in Excel with given number of raws and dynamic number of columns (each week a new column should be added, currently I'm doing it manually). I want to automate it and create a script that will extend every raw range to the next column (namely the range from A2 to C2 should become from A2 to D2), and so on (such that running script N times will result in extending a table to N columns further). By "extending" I mean extending formulas, since each cell in my tables contains any formula. Is there any way to do it via VBA?
I can't just record the corresponding macro because I have now idea how to specify that I don't want to link it with any specific range, but instead always extend just to one column right.
Any help and examples will be very appreciative.
You dont need VBA to do this. Use dynamic defined names and reference them in your formulas. For example, if you add a named range and add this in the refersTo dialog
=OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1)
your range will automatically expand from cell A1 (as long as there are no blank cells in column A or Row 1). You can then use that named range in your formulae.
More here http://www.excel-easy.com/examples/dynamic-named-range.html

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

Getpivotdata Pivot location based on a value in another cell.

GETPIVOTDATA("Data",{Dynamic reference to another sheet},"Field","Item")
I'm trying to get my formula to be super dynamic. I have 5 - 10 different worksheets with pivot tables on them. I have the rest of the formulas setup to get their "FIELD" and "ITEM" off of a small table of data that can change. I have tried named ranges but again cannot get it to work. The only thing in the getpivot i cannot make dynamic is the location of the pivot table. I was shown the CHOOSE(1,"STRINGS") function, but I do not know how many pivots there will be. I've used the INDIRECT() with vlookups. I'm fresh out of ideas any help welcome.
Using a named range to refer to the pivot table should work. However, make sure the named range only refers to a single cell within the pivot table. If it refers to a range, or the whole table it will not work.
EDIT
If you want to pull the pivottable from another cell, you'll need to use the INDIRECT function
=GETPIVOTDATA("Amount",INDIRECT($Q$2),"Name","Jack","Month","Feb")
Cell $Q$2 can contain either a named range referring to the pivot table or a cell reference. In either event, you will also need to specify the worksheet if you're working across multiple sheets. eg, $Q$2 could contain
Pivots!$A$1

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.