|
Another very useful method is the Autofill
method. This method performs an autofill on the cells in the specified
range with a series of items including numbers, days of week, months of
year and more. The format is
Expression.AutoFill(Destination, Type)
Where Expression can be an object or a
variable that returns and object. Destination means the required Range
object of the cells to be filled. The destination must include the
source range. Type means type of series, such as days of week, month of
year and more. The AutoFill type constant is something like
xlFillWeekdays, XlFillDays, XlFillMonths and more.
¡¡
Example 20.6
Private Sub CommandButton1_Click()
Range(¡°A1¡±)=1
Range(¡°A2¡±)=2
Range("A1:A2").AutoFill
Destination:=Range("A1:A10")
End Sub
In this example, the source range is A1 to
A2. When the user clicks on the command button, the program will first
fill cell A1 with 1 and cell A2 will 2, and then automatically fills the
Range A1 to A10 with a series of numbers from 1 to 10.
Example 20.7
Private Sub CommandButton1_Click()
Cells(1, 1).Value = "monday"
Cells(2, 1).Value = "Tuesday"
Range("A1:A2").AutoFill
Destination:=Range("A1:A10"), Type:=xlFillDays
End Sub
Example 20.8
This example allows the user to select the
range of cells to be automatically filled using the Autofill method.
This can be achieved with the use of the InputBox. Since each time we
want to autofill a new range, we need to clear the contents of the
entire worksheet using the Sheet1.Cells.ClearContents statement.
Private Sub CommandButton1_Click()
Dim selectedRng As String
Sheet1.Cells.ClearContents
selectedRng = InputBox("Enter your
range")
Range("A1") = 1
Range("A2") = 2
Range("A1:A2").AutoFill Destination:=Range(selectedRng)
End Sub
|