如何使用VBA在Excel中自动填充单元格?

我有一些数据在第一列,一些计算字段后,我想用自动填充其余的行与第一行相同的规则。

input/计算数据的总行数,列数是已知的,我将非常感谢这个数据的一个工作示例:

| A | B | C | D | E | ---------------------------------------------- 1 | 3 | 1 | =A1+B1 | =A1*B1 | =sum(C1:D1)| 2 | 4 | 4 | | | | 3 | 5 | 23 | | | | 4 | 42 | 4 | | | | 5 | 7 | 4 | | | | 

真正的数据通常有10K行和30列。 当我试图做手动有时得到错误Selection is too large 。 我告诉这个,因为一般的解决scheme可能无法使用VBA,但如果我知道如何自动填充这个例子,我会按需要做每列。 Excel的版本是2000年,这不是我的错:)

 sub copydown() Range("c1:e" & Range("a" & activesheet.rows.count).End(xlUp).Row).FillDown end sub 

基本的,但它应该给你一些东西来build立和它在Excel 2003(我有最古老的)。

 Option Explicit Public Sub CopyFormulaeExample() On Error GoTo Handle_Exception Dim lastRow As Long Dim wrkSheet As Excel.Worksheet 'Book and sheet names hard-coded for this example Set wrkSheet = Application.Workbooks("Book1").Worksheets("Sheet1") 'Get the index of the last row used lastRow = wrkSheet.UsedRange.End(xlDown).Row 'Copy the cells containing the formulae; also hard-coded for this example Range("C1:E1").Select Selection.Copy 'Paste the selection to the range of interest Range("C2:E" + CStr(lastRow)).PasteSpecial xlPasteAll 'Alternative approach Range("C1:E1").Copy Range("C2:E" + CStr(lastRow)) 'Release memory and exit method Set wrkSheet = Nothing Exit Sub Handle_Exception: Set wrkSheet = Nothing MsgBox "An error has been found: " + Err.Description End Sub 

使用复制(ctrl-D)function。

select单元格c1-e1,然后一路向下(如果有10,000行数据,则select的单元格范围是c1-e10000)。

按下Ctrl-D。

这将单元格内容(您的公式)复制到它下面的所有单元格。

http://www.google.com/search?q=using+excel+ctrl-d

以下是我做的,当我做到了:)

 Ctrl+C <-- Ctrl+Shift+Down --> Ctrl+Shift+Up Ctrl+V 

这在我看来是最有效的方法。 没有什么可以阻止你把这个包装到一个macros中,并分配一个方便的键绑定。