[英]VBA: Macro to loop through rows and Autofill
編輯:如果我想自動填充這些單元格,下面的代碼可以工作嗎?
Sub BC_Edit()
' Define width and height of table
Dim datasetWidth, datasetHeight As Integer
' Find values for width and height of table
datasetWidth = Range("A1").End(xlToRight).Column
datasetHeight = Range("A1").End(xlDown).Row
' Loop over each column
For x = 1 To datasetWidth
Set sourceRange = Cells(2,x)
Set fillRange = Range(Cells(3, x), Cells(datasetHeight, x))
sourceRange.AutoFill Destination:=fillRange
Next
End Sub
我正在處理幾個非常大的數據集-每個數據集大約3000行乘4000列。 盡管Excel可能不是最佳的工具,但我已經圍繞數據構建了大量的基礎結構,無法遷移到其他框架。 我正在使用Excel 2007。
在一個特定的工作表中,當我嘗試使用公式自動填充時,我已將整個第二列B(3000 x 1)輸入該行,並將該列復制並粘貼到3998選擇項或該選擇項的某些部分中的剩余3000中,Excel給我一個內存/資源錯誤。 因此,我想遍歷每一行並在所有列之間自動填充。 (換句話說,我想使用A2自動填充A3:A4000,使用B2自動填充B3:B4000,依此類推。)也許這將有助於解決內存問題。 我將如何編寫宏來完成此任務?
我希望就此問題提供任何建議,並在可能的情況下為適當的VBA代碼提供幫助。
這是一個宏的非常基本的示例,該宏將第2列以下的列設置為第2列的公式。最好將其附加到按鈕或類似的東西上,而不是每次打開工作表時都運行。
Sub Button_Click()
' Define width and height of table
Dim datasetWidth, datasetHeight As Integer
' Find values for width and height of table
datasetWidth = Range("A1").End(xlToRight).Column
datasetHeight = Range("A1").End(xlDown).Row
' Loop over each column
For x = 1 To datasetWidth
' From row 3 to the height of data, set the formula of the cells to
' the formula contained in row 2 of that column
Range(Cells(3, x), Cells(datasetHeight, x)).Formula = Cells(2, x).Formula
Next
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.