如何更改代码以在第7行的每列中粘贴公式?

时间:2019-03-22 18:30:04

标签: excel vba

我创建了以下代码,因此可以将公式每隔AK列的第7行粘贴一次,直到D列的最后一个空单元格都可以正常工作,但是,我需要执行相同的操作,但接下来的30列必须正确(从AK到BO)。

能否请您指导我如何更改此代码?另外,我认为定义LastRow的代码行有问题:LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row。它正在工作,但有时它会将一些奇怪的结果粘贴到AJ列的最后一行

每本指南都很受赞赏。

谢谢。

  Dim sht As Worksheet
  Set sht = Worksheets("Data")

    With sht
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    Dim r As Integer

    For r = 8 To LastRow Step 7

       .Cells(r, "AK").Select
        ActiveCell.FormulaR1C1 = _
        "=IF(AND(R[4]C[-32]="""",R[5]C[-32]=""""),R7C[-32]&"","","""")"

    Next
    End With

1 个答案:

答案 0 :(得分:0)

我不确定是否可以理解,但是您可以修改以下代码并尝试:

Option Explicit

Sub test()

  Dim Row As Long, Column As Long, LastRow As Long

  With ThisWorkbook.Worksheets("Data")

        LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row

        For Row = 8 To LastRow Step 7

            For Column = 37 To 67 Step 30

                .Cells(Row, Column).FormulaR1C1 = "=IF(AND(R[4]C[-32]="""",R[5]C[-32]=""""),R7C[-32]&"","","""")"

            Next Column

        Next Row

    End With

End Sub