我有一个大约75个工作表的工作簿,所有工作表都包含每月更新的不同数据。某些数据会自动更新,但是在几乎每个工作表中,都需要拖动公式。我需要在每个工作表中将公式拖到30行。
因此,我想循环遍历每个工作表,然后浏览包含要拖动的公式的每列。我已经在列的第1行中标记了每列用字母" F"拖动的,以便我可以放一个If语句以仅拖动这些列。
我的问题现在是我不知道如何选择带有公式的列的最后一个单元格,然后将其拖动30行。
Sub Drag_Formulas()
'Number of Worksheets
Dim i As Integer
Dim ws_num As Integer
ws_num = ThisWorkbook.Worksheets.Count
'Number of columns
Dim c As Integer
'Loop 1
For i = 1 To ws_num
ThisWorkbook.Worksheets(i).Activate
For c = 1 To 105
If Cells(1, c).Value = "F" Then
Cells(20000, c).Select 'I used 20000 since no worksheet has data going as far as 20000, so that way I am sure to get the last cell with data
Selection.End(xlUp).Select
Selection.Copy
Else
Next c
End If
Next c
End Sub
因此,我到了使用列的公式复制最后一个单元格,但是我不知道如何将其拖动30行,因为使用此代码,我不知道最后一个单元格。
感谢您的帮助!
如前所述,当前宏实际上没有做任何事情。但是,假设每列的公式在第2行中,并且您想将其拖到该列中的最后一行,则可以使用以下内容。
Sub drag()
Dim i As Long, col As Long, lastCol As Long, lastRow As Long
Dim copyRowAmt
Dim ws As Worksheet
Dim fmlaCell As Range
copyRowAmt = 30
For Each ws In ThisWorkbook.Worksheets
With ws
' This assumes your column headers are in row 1,
' to get the total number of columns dynamically
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For col = 1 To lastCol
If .Cells(1, col).Value = "F" Then
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
.Range(.Cells(lastRow, col), .Cells(lastRow + copyRowAmt, col)).Formula = _
.Cells(lastRow, col).Formula
End If
Next col
End With
Next ws
End Sub
请注意,这还避免了使用.Select
`.Activate`