如何修复“ VBA只运行一部分代码而忽略另一部分代码”

时间:2018-12-27 16:42:46

标签: excel-vba

当我按F8键逐行运行并检查代码时,它工作正常。但是,当我使用F5运行整个子程序时,VBA只会运行一个For-Next循环。如果有人遇到同样的问题?

Sub InputPurchaseData()

Dim rngPurchaseInfoLoop As Range, rngPurchaseInfo As Range
Dim rngPurchaseItemsFieldLoop As Range, rngPurchaseItemsField As Range
Dim rngPurchaseItemsLoop As Range, rngPurchaseItems As Range
Dim rngPurchaseDataFieldLoop As Range, rngPurchaseDataField As Range
Dim rngPurchaseDataRowLoop As Range, rngPurchaseDataRow As Range
Dim lngPurchaseItemRow As Long, lngPurchaseDataRow As Long
Dim msgOption As Boolean
Dim strPurchaseNumber As String

wsPurchaseOrder.Activate

Set rngPurchaseInfo = wsPurchaseOrder.Range("A1").CurrentRegion

Set rngPurchaseItemsField = wsPurchaseOrder.Range("B1", Range("B1048576").End(xlUp)).Find(What:="ITEM", _
        MatchCase:=True, LookAt:=xlWhole)

Set rngPurchaseItems = Range(rngPurchaseItemsField.Offset(2, 0), Range("B1048576").End(xlUp).Offset(-1, 0))

Set rngPurchaseItemsField = Range(rngPurchaseItemsField, rngPurchaseItemsField.End(xlToRight))

Set rngPurchaseDataField = wsPurchaseData.Range("A1").CurrentRegion.Resize(1)

Set rngPurchaseDataRow = wsPurchaseData.Range("A1").CurrentRegion.Resize(, 1)
For Each rngPurchaseItemsLoop In rngPurchaseItems
    If IsNumeric(rngPurchaseItemsLoop.Value) Then
        lngPurchaseItemRow = rngPurchaseItemsLoop.Row - rngPurchaseItems.Resize(1).Row + 2

    For Each rngPurchaseInfoLoop In rngPurchaseInfo

        Set rngPurchaseDataFieldLoop = rngPurchaseDataField.Find(What:=rngPurchaseInfoLoop.Value, MatchCase:=True, LookAt:=xlWhole)

        If Not rngPurchaseDataFieldLoop Is Nothing Then _
            rngPurchaseDataFieldLoop.Offset(rngPurchaseDataRow.Rows.Count).Value = rngPurchaseInfoLoop.Offset(, 1).Value

    Next rngPurchaseInfoLoop
    For Each rngPurchaseItemsFieldLoop In rngPurchaseItemsField

        Set rngPurchaseDataFieldLoop = rngPurchaseDataField.Find(What:=rngPurchaseItemsFieldLoop.Value, MatchCase:=True, LookAt:=xlWhole)

        If Not rngPurchaseDataFieldLoop Is Nothing Then _
            rngPurchaseDataFieldLoop.Offset(rngPurchaseDataRow.Rows.Count).Value = rngPurchaseItemsFieldLoop.Offset(lngPurchaseItemRow).Value

    Next rngPurchaseItemsFieldLoop

    End If

Next rngPurchaseItemsLoop

End Sub

0 个答案:

没有答案