VBA 宏 - 使用一个工作表中的数据自动填充多个工作表的公式
VBA Macro - Automatic formulas autofill for multiple worksheets with data from one worksheet
我目前在使用 VBA 自动填充公式时遇到问题。我将此代码用于工作表 A 以自动填充列 S & T 关于列 A 的长度:
Sub AutoFill()
Dim rc As Long
rc = Range("A" & Rows.Count).End(xlUp).Row
Range("S2:T2").AutoFill Range("S2:T" & rc)
End Sub
代码在工作表 A 中运行良好。我的问题出在工作表 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
我知道这样不好,但你可能更能理解我要实现的逻辑。
**我还没有掌握VBA,希望快点好起来!
感谢您的帮助!
均线
找到解决问题的方法:
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
我目前在使用 VBA 自动填充公式时遇到问题。我将此代码用于工作表 A 以自动填充列 S & T 关于列 A 的长度:
Sub AutoFill()
Dim rc As Long
rc = Range("A" & Rows.Count).End(xlUp).Row
Range("S2:T2").AutoFill Range("S2:T" & rc)
End Sub
代码在工作表 A 中运行良好。我的问题出在工作表 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
我知道这样不好,但你可能更能理解我要实现的逻辑。 **我还没有掌握VBA,希望快点好起来!
感谢您的帮助!
均线
找到解决问题的方法:
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