Values & Formulae
This part is very easy! To set a cells value, simply use the Value property:
ActiveCell.Value = "hello!"
To specify a formulae, you use the Formula property:
Range("A2").Formula = "=2+2"
Simple!
AutoFill
Another very useful function in Excel is the ability to write one formula, and drag it across multiple cells, with Excel automatically changing cell references as required. You can use this in your Macros too, by using the AutoFill function. AutoFill takes the following parameters:
AutoFill DestinationRange, Type
Note that instead of passing "A2" in DestinationRange, you pass the Range object. This lets you use either Range or Cells to get the cells you want. So, to AutoFill from A1 to A2 through to A6 (A2:A6), you simply pass:
Range("A1").AutoFill Range("A1:A6"), xlFillDefault
Note that you must include the source range in the destination! In some cases, such as incrementing numbers, you will need to give Excel two cells so that it can work out the interval:
Range("A1:A2").AutoFill Range("A1:A6"), xlFillDefault
If the AutoFill doesn't do what you want by default, you can specify a different AutoFill method in the place of xlFillDefault:
AutoFill Types |
xlFillCopy |
xlFillDays |
xlFillDefault |
xlFillFormats |
xlFillMonths |
xlFillSeries |
xlFillValues |
xlFillWeekdays |
xlFillYears |
xlGrowthTrend |
xlLinearTrend |
For more info, take a look at the Excel VBA help.
Comments