簡體   English   中英

使用 VBA 自動填充(導入新導出時擴展公式)

[英]Autofill with VBA (Extending formulas when a new export is imported)

情況如下。 通過 OLEDB 連接導入一個大的出口槽。 前 40 列是導出,接下來的 10 列包含幫助公式。

在打開時,我希望它使用自動填充來復制公式,因此它們會在新導出時命中所有內容。

我目前嘗試使用的代碼質量非常低(錄制的宏),這根本不專業,但我正在嘗試學習如何在不錄制的情況下從頭開始設置。

代碼:

Sub Macro1()
Sheets("DumpFollowUp").Select
Range("AT128432").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFill Destination:=Range("AT128512:BN128787")
Range("AT128512:BN128787").Select
End Sub

所以如上所述,直到 BN 的列 AT 包含我需要擴展的公式。 刷新后將在打開工作簿事件中使用此宏,因此它可以正確擴展(已加載新導出)。

以下內容可能對您有所幫助,我只是刪除了任何 Select 語句:

Private Sub Workbook_Open()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("DumpFollowUp")
'declare and set the worksheet you are working with, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
LastFormula = ws.Cells(1, "AT").End(xlDown).Row
'get the last row with formulas on Column AT
Application.Calculation = xlCalculationManual
ws.Range(ws.Cells(LastFormula, "AT"), ws.Cells(LastRow, "BN")).FillDown
'fill down from LastFormula to the last row with data
Application.Calculation = xlCalculationAutomatic
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM