首页 文章

使用LastRow复制粘贴范围

提问于
浏览
1

我有一个问题,如果从 A7 开始直到LastRow行有数据,如何将公式从某个指定范围复制到指定的FirstRow到LastRow .

如果从 A7 到LastRow有数据, H6:J6 中的公式应该作为公式从 H7:J7 粘贴到LastRow .

现在的问题是,如果从 A7 开始,行是空的,它将复制 H5:J5 中的公式 . 有没有我可以使用的代码,如果 A7 前是空的,根本没有复制公式?也许将FirstRow定义为固定或其他东西 .

Sub CopyFormulaIF()

Dim myLastRow As Long
Dim myCol As Long
Dim WB As Workbook
Dim WS As Worksheet

Set WB = ThisWorkbook
Set WS = WB.Sheets("Tabelle1")

'Screen update in 0 seconds
Application.ScreenUpdating = 0

With WS
    myLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With

For myCol = 8 To 10                                              
    Cells(6, myCol).Copy                                        
    Range(Cells(7, myCol), Cells(myLastRow, myCol)).PasteSpecial Paste:=xlFormulas 
Application.CutCopyMode = False                             
Next myCol

End Sub

样品:
enter image description here

非常感谢你们

1 回答

  • 2

    你可以做到这一点

    Option Explicit
    
    Sub CopyFormulaIF()
        Dim myLastRow As Long
        Dim myCol As Long
    
        'Screen update in 0 seconds Application.ScreenUpdating = 0
    
        With ThisWorkbook.Sheets("Tabelle1")
            myLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            If myLastRow < 7 Then Exit Sub
    
            For myCol = 8 To 10
                .Range(.Cells(7, myCol), .Cells(myLastRow, myCol)).FormulaR1C1 = .Cells(6, myCol).FormulaR1C1
            Next
        End With
    End Sub
    

相关问题