通过F5执行时省略了VBA循环,但是使用F8逐步执行时执行

时间:2017-03-24 07:27:58

标签: excel-vba loops vba excel

我在Excel工作表的特定行中有一些值(从公式派生)。我正在尝试使用.FillDown Range方法将所有列拖动到一定数量的行,直到所有列。

Sub RawDataPreparation()

    Dim v As Long
    Dim LastRow As Long
    Dim lastCol As Integer
    Dim c As Integer

    c = ThisWorkbook.Sheets("Metadata").Range("B10")

    Dim ColName() As String
    ReDim ColName(c)

    Dim strFormulas() As Variant
    ReDim strFormulas(c)

    Dim lastColumn As Integer
    With ThisWorkbook.Sheets("Raw Data")
        lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With

    For i = 0 To c - 1
        On Error Resume Next
        ColName(i) = ThisWorkbook.Sheets("Metadata").Range("C" & i + 10) 'copy the value corresponding to column C10:C18 in Metadata sheet to the array
        ThisWorkbook.Sheets("Raw Data").Cells(1, lastColumn + 1 + i).Value = ColName(i)
    Next i

    With ThisWorkbook.Sheets("Raw Data")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    For j = 0 To c - 1
        On Error Resume Next
        strFormulas(j) = ThisWorkbook.Sheets("Metadata").Range("D" & j + 10) 'copy the formulas corresponding to column D10:D18 in Metadata sheet to the array
        With ThisWorkbook.Sheets("Raw Data")
            .Cells(2, lastColumn + 1 + j).Formula = strFormulas(j)
        End With
    Next j

    'Code to drag down the formula till last row.
    For k = 0 To c - 1
        ThisWorkbook.Sheets("Raw Data").Range(Cells(2, lastColumn + 1 + k), Cells(LastRow, lastColumn + 1 + k)).FillDown
    Next k

End Sub

当我使用F8(逐步)执行时,正在执行最后一个循环(将公式拖到最后一行)并给出预期结果。但是在使用F5执行整个Sub RawDataPreparation时,最后一个循环将被省略。

我无法理解这种行为。谁能说明为什么会这样?

2 个答案:

答案 0 :(得分:1)

您需要限定对CellsRangeRows等的引用,除非您知道您的ActiveSheet是被引用的表单到。

所以将你的上一个陈述改为

   ThisWorkbook.Sheets("Raw Data").Range(ThisWorkbook.Sheets("Raw Data").Cells(2, lastColumn + 1 + k), ThisWorkbook.Sheets("Raw Data").Cells(LastRow, lastColumn + 1 + k)).FillDown

或者,为了让它更容易阅读:

   With ThisWorkbook.Sheets("Raw Data")
       .Range(.Cells(2, lastColumn + 1 + k), .Cells(LastRow, lastColumn + 1 + k)).FillDown
   End With

答案 1 :(得分:0)

为了使你的代码工作,最后一个循环应该像这样定义范围: -

Dim k As Long

For k = 0 To C - 1
    With ThisWorkbook.Sheets("Raw Data")
        .Range(.Cells(2, lastColumn + 1 + k), .Cells(lastRow, lastColumn + 1 + k)).FillDown
    End With
Next k

观察定义范围的Cells之前的时间段。这段时间使参考文献参考"原始数据"在您的代码中,在没有任何空格的情况下,它们引用ActiveSheet。所以你会得到一个不同的结果,而不是取决于你如何触发代码,但当时是活动表。

无论如何,为了证明我的观点,我必须几乎重写你的代码。我继续完成这项工作,就在这里。

Sub RawDataPreparation()
    ' 24 Mar 2017

    Dim WsMetadata As Worksheet
    Dim WsRawData As Worksheet
    Dim firstMetaRow As Long, TransferRow As Long
    Dim nextCol As Integer
    Dim lastRow As Long
    Dim R As Long, C As Long

    firstMetaRow = 10    ' set this value here instead of in the code
    Set WsMetadata = ThisWorkbook.Sheets("Metadata")
    TransferRow = CLng(Val(WsMetadata.Range("B10").Value)) - 1
    ' B10 can't have a value < 1. In your example it should be 9
    If TransferRow < 0 Then Exit Sub

    Set WsRawData = ThisWorkbook.Sheets("Raw Data")
    With WsRawData
        nextCol = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
    End With

    ' transpose columns C and D of WsMetaData to rows 1 and 2 in WsRawData
    For R = firstMetaRow To (firstMetaRow + TransferRow)
        With WsMetadata
            .Cells(R, "C").Copy Destination:=WsRawData.Cells(1, (nextCol + R - firstMetaRow))
            .Cells(R, "D").Copy Destination:=WsRawData.Cells(2, (nextCol + R - firstMetaRow))
        End With
    Next R

    With WsRawData
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range(.Cells(2, nextCol), .Cells(lastRow, nextCol + TransferRow)).FillDown
    End With
End Sub

它有效,但不幸的是它可能无法完成你想象的工作。从元数据中复制的公式包含对该表单的引用,一旦转换范围,该表单就不能以1:1进行转换。您可能必须满足于粘贴值或在填充之前翻译公式。