使用一个工作表中的数据自动填充多个工作表的公式

问题描述 投票:0回答:1

我在工作表 A 中使用此代码来自动填充有关 A 列长度的 S 列和 T 列:

Sub AutoFill()

Dim rc As Long

rc = Range("A" & Rows.Count).End(xlUp).Row
Range("S2:T2").AutoFill Range("S2:T" & rc)

End Sub

我的问题出在工作表 B 中。我的 A 列到 E 列的公式位于 A2:E2 中。我想扩展工作表 B 中的公式,但仅限于工作表 A 中 A 列的长度。我想要一个宏来完成所有这些操作。

我试过这个:

Sub AutoFill()

Dim rc As Long

rc = Range("A" & Rows.Count).End(xlUp).Row
Range("S2:T2").AutoFill Range("S2:T" & rc)
rc = Range("A" & Rows.Count).End(xlUp).Row
Range("WorksheetB!A2:WorksheetB!E2").AutoFill Range("WorksheetB!A2:WorksheetB!E" & rc)
 
End Sub

但它不起作用。

excel vba excel-formula worksheet-function worksheet
1个回答
0
投票

找到解决问题的方法:

Sub AutoFill()

Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim sh3 As Worksheet
Dim sh4 As Worksheet
Dim sh5 As Worksheet
Dim rc As Long
'Dim wkb As Workbook

'Set wkb = Workbooks("Ontario-NewRecurrentCustomersV2") '-> best to call workbooks by name, as opposed to "ActiveWorkbook", also best to set it to object

With ActiveWorkbook

    Set sh1 = .Sheets("CleanDataWeek")
    Set sh2 = .Sheets("CleanDataWeek(P-1)")
    Set sh3 = .Sheets("CalculatedDataWeek")
    Set sh4 = .Sheets("CalculatedDataWeek(P-1)")
    Set sh5 = .Sheets("RawData")

    rc = sh5.Range("A" & Rows.Count).End(xlUp).Row
    sh5.Range("S2:T2").AutoFill sh5.Range("S2:T" & rc)

    rc = sh5.Range("A" & Rows.Count).End(xlUp).Row
    sh1.Range("A2:E2").AutoFill sh1.Range("A2:E" & rc)

    rc = sh5.Range("A" & Rows.Count).End(xlUp).Row
    sh2.Range("A2:E2").AutoFill sh2.Range("A2:E" & rc)

    rc = sh5.Range("A" & Rows.Count).End(xlUp).Row
    sh3.Range("A2:F2").AutoFill sh3.Range("A2:F" & rc)

    rc = sh5.Range("A" & Rows.Count).End(xlUp).Row
    sh4.Range("A2:F2").AutoFill sh4.Range("A2:F" & rc)

End With

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.