我已经接近完成了我的目标。我基本上只需要在列的末尾直接需要工作簿的所有工作表中的每一列。我的问题是答案都出现在第一张纸上而不是相关的纸张上。我无法弄清楚我需要改变什么!
这是我与
合作的内容Private Sub Workbook_Open()
Dim ws As Worksheet
Dim Lastrow As Long, Lastcol As Long
For Each ws In ThisWorkbook.Worksheets
Lastrow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
Lastcol = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Range(Cells(Lastrow, "D"), Cells(Lastrow, Lastcol)).FormulaR1C1 = "=SUM(R1C:R" & Lastrow - 1 & "C)"
Next ws
End Sub
答案 0 :(得分:3)
将Range(Cells(Lastrow, "D"), ...
替换为
ws.Range(ws.Cells(Lastrow, "D"), ...
由于您没有指定工作表,因此在活动工作表中解释了Range
。
答案 1 :(得分:3)
试试这个:
Private Sub Workbook_Open()
Dim ws As Worksheet
Dim Lastrow As Long, Lastcol As Long
For Each ws In ThisWorkbook.Worksheets
Lastrow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
Lastcol = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
ws.Activate
Range(Cells(Lastrow, "D"), Cells(Lastrow, Lastcol)).FormulaR1C1 = "=SUM(R1C:R" & Lastrow - 1 & "C)"
Next ws
End Sub
或(完成@John Coleman的回答)
Sub test()
Dim ws As Worksheet
Dim Lastrow As Long, Lastcol As Long
For Each ws In ThisWorkbook.Sheets
Lastrow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
Lastcol = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
ws.Range(ws.Cells(Lastrow, "A"), ws.Cells(Lastrow, Lastcol)).FormulaR1C1 = "=SUM(R1C:R" & Lastrow - 1 & "C)"
Next ws
End Sub