制作自动填充范围

时间:2016-02-13 15:59:58

标签: vba excel-vba excel

我试图在VBA上制作这个自动填充范围,但我不知道什么是错误的,甚至是否有效。 它应该根据每列的第一和第二单元填充所有范围A1到Z20。 如果其他人可以做到这一点或有什么问题请修理它。

Sub test()

Dim i As Range
Dim a As Long

Do While Not Range("Z20").Value > 0
If a = 0 Then
a = 1
Else
End If

For Each i In Range("A:Z")
a = a + 1
i = Range(Cells(1, a), Cells(20, a))
Cells(1, a).Value = 1
Cells(2, a).Value = 2

Set SourceRange = Range(Cells(1, a), Cells(2, a))
Set fillRange = Range(i).Columns(a)
SourceRange.AutoFill Destination:=fillRange
Next
Loop
End Sub

1 个答案:

答案 0 :(得分:0)

Option Explicit
Sub test()

Dim c As Range
Dim a As Long
Dim sourceRange As Range

a = 1
Do While a <> 20
ActiveSheet.Cells(1, a) = 1
ActiveSheet.Cells(2, a) = 2
Set c = ActiveSheet.Range(Cells(1, a), Cells(20, a))

Set sourceRange = ActiveSheet.Range(Cells(1, a), Cells(2, a))

sourceRange.AutoFill Destination:=c

a = a + 1

Loop
End Sub