MACRO可以单步执行,但通过按钮运行时会中断

时间:2018-04-05 20:40:12

标签: excel vba excel-vba

我创建了一个宏,它从工作表中读取数据并创建日记帐分录。由于电子表格的一些更改,我不得不更新宏,但现在我的宏不起作用。

如果我单步执行整个操作或者在VSB屏幕中点击播放按钮,我的宏就会起作用。但是,如果我点击嵌入电子表格的宏按钮,它会中断。我相信它的突破是因为它跳过了这一部分:

  If Cells(iRow2, 1) = "CCC $" Then
    wsUp.Cells(iRow2, 1).Value = "CC"
    ElseIf Cells(iRow2, 1) = "DDD $" Then

我不确定为什么只有当我点击嵌入式按钮而不是从VSB屏幕运行它时才跳过它。

Sub CreateAllocations_JEs()

Dim iRow As Integer, iCol As Integer, iRow2 As Integer
Dim sEntity As String, sEnt2 As String, sVal1 As String, sEnt3 As String, sDesc2 As String
Dim wsEntry As Worksheet
Dim wsUp As Worksheet
Dim wsInst As Worksheet
Set wsInst = Worksheets("Instructions")
Set wsEntry = Worksheets("Entries")
Set wsUp = Worksheets("Sheet1")
Dim lastrow As Long
Dim sRange As Range
Dim sQLNE As Long

''' Creates expense for holdings
For iRow = 6 To 35
    lastrow = wsUp.Cells(Rows.Count, "A").End(xlUp).Row
    sEntity = wsEntry.Range("D5").Value
    sAcct = wsEntry.Range("N" & iRow).Value
    sAcct2 = wsEntry.Range("M" & iRow).Value
    sDesc = wsEntry.Range("O" & iRow).Value
    vsum = Application.WorksheetFunction.Sum(wsEntry.Range("E" & iRow & ":J" & iRow))
    If vsum > 0 Then
    wsUp.Range("A" & lastrow + 1).Value = sEntity
    wsUp.Range("J" & lastrow + 1).Value = vsum
    wsUp.Range("G" & lastrow + 1).Value = sAcct
''''' Adds description column using the companies that have payables
    sDesc2 = ""
    End If
    For iCol = 5 To 12
        If wsEntry.Cells(iRow, iCol) > 0 Then
        sEnt3 = wsEntry.Cells(5, iCol).Value
            If sDesc2 <> "" Then
            sDesc2 = sDesc2 & ", "
            End If
        sDesc2 = sDesc2 & sEnt3
        End If

    Next iCol
    wsUp.Range("M" & lastrow + 1).Value = sDesc & sDesc2

    '''''' Creates receivable for holdings and related fields
    For iCol = 5 To 10
        If wsEntry.Cells(iRow, iCol) > "0" Then
        sVal1 = wsEntry.Cells(iRow, iCol).Value
        sDesc = wsEntry.Range("O" & iRow).Value
        sEnt3 = wsEntry.Cells(5, iCol).Value
        lastrow = wsUp.Cells(Rows.Count, "A").End(xlUp).Row
        wsUp.Range("A" & lastrow + 1).Value = sEntity
        wsUp.Range("I" & lastrow + 1).Value = sVal1
        vRec = Application.WorksheetFunction.Index(Sheets("IC accounts").Range("C:C"), Application.WorksheetFunction.Match(Sheets("Entries").Cells(5, iCol), Sheets("IC accounts").Range("B:B"), 0), 1)
        wsUp.Range("G" & lastrow + 1).Value = vRec
        wsUp.Range("M" & lastrow + 1).Value = sDesc & sEnt3
        End If
    Next iCol

    ''''Creates the payables and expense in other companies
    For iCol = 5 To 12

        If wsEntry.Cells(iRow, iCol) > "0" Then
        sEnt2 = wsEntry.Cells(5, iCol).Value
        sval2 = wsEntry.Cells(iRow, iCol).Value
        lastrow = wsUp.Cells(Rows.Count, "A").End(xlUp).Row
        wsUp.Range("A" & lastrow + 1, "A" & lastrow + 2).Value = sEnt2
            If wsUp.Range("A" & lastrow + 1).Value = "AAA $" Then
                wsUp.Range("J" & lastrow + 1).Value = sval2
                wsUp.Range("I" & lastrow + 2).Value = sval2
                wsUp.Range("G" & lastrow + 1).Value = sAcct2
                wsUp.Range("G" & lastrow + 2).Value = "00-1320001"
            ElseIf wsUp.Range("A" & lastrow + 1).Value = "BBB $" Then
                wsUp.Range("J" & lastrow + 1).Value = sval2
                wsUp.Range("I" & lastrow + 2).Value = sval2
                wsUp.Range("G" & lastrow + 1).Value = sAcct2
                wsUp.Range("G" & lastrow + 2).Value = "00-1320002"
            Else
                wsUp.Range("I" & lastrow + 1).Value = sval2
                wsUp.Range("J" & lastrow + 2).Value = sval2
                wsUp.Range("G" & lastrow + 1).Value = sAcct2
                wsUp.Range("G" & lastrow + 2).Value = "00-4100040"
            End If
        wsUp.Range("M" & lastrow + 1, "M" & lastrow + 2).Value = sDesc & sEntity
        End If


    Next iCol

Next iRow

    lastrow = wsUp.Cells(Rows.Count, "A").End(xlUp).Row
    For iRow2 = 2 To lastrow

    If Cells(iRow2, 1) = "CCC $" Then
    wsUp.Cells(iRow2, 1).Value = "CC"
    ElseIf Cells(iRow2, 1) = "DDD $" Then
    wsUp.Cells(iRow2, 1).Value = "DD"
    ElseIf Cells(iRow2, 1) = "EEE $" Then
    wsUp.Cells(iRow2, 1).Value = "EE"
    ElseIf Cells(iRow2, 1) = "FFF $" Then
    wsUp.Cells(iRow2, 1).Value = "FF"
    ElseIf Cells(iRow2, 1) = "GGG $" Then
    wsUp.Cells(iRow2, 1).Value = "GG"
    ElseIf Cells(iRow2, 1) = "HHH $" Then
    wsUp.Cells(iRow2, 1).Value = "LLL"
    ElseIf Cells(iRow2, 1) = "AAA $" Then
    wsUp.Cells(iRow2, 1).Value = "LLL"
    ElseIf Cells(iRow2, 1) = "LLL $" Then
    wsUp.Cells(iRow2, 1).Value = "LLL"
    ElseIf Cells(iRow2, 1) = "JJJ $" Then
    wsUp.Cells(iRow2, 1).Value = "JJ"

    End If

    wsUp.Activate

代码中断。我相信,因为跳过上面的部分。

    vCN = 
    Application.WorksheetFunction.Index(Sheets("Company").Range("B:B"), 
    Application.WorksheetFunction.Match(Sheets("Sheet1").Cells(iRow2, 1), 
    Sheets("Company").Range("A:A"), 0), 1)
    wsUp.Range("B" & iRow2).Value = vCN
    vAN = Application.WorksheetFunction.Index(Sheets("COA").Range("B:B"), 
    Application.WorksheetFunction.Match(Sheets("Sheet1").Cells(iRow2, 7), 
    Sheets("COA").Range("A:A"), 0), 1)
    wsUp.Range("H" & iRow2).Value = vAN
    sQLNE = wsUp.Cells(Rows.Count, "N").End(xlUp).Row
    wsUp.Range("N" & iRow2).Value = sQLNE
    wsUp.Range("S" & iRow2).Value = wsUp.Range("I" & iRow2).Value
    wsUp.Range("T" & iRow2).Value = wsUp.Range("J" & iRow2).Value
    Next iRow2



lastrow = wsUp.Cells(Rows.Count, "A").End(xlUp).Row
sBatch = wsInst.Cells(8, 2).Value
sMonth = wsInst.Cells(6, 2).Value
sYear = wsInst.Cells(7, 2).Value
sDate = wsInst.Cells(5, 2).Value
sRef = sBatch & sMonth & sYear
wsUp.Range("C2", "C" & lastrow).Value = sRef
wsUp.Range("f2", "F" & lastrow).Value = sRef
wsUp.Range("D2", "D" & lastrow).Value = "1"
wsUp.Range("e2", "E" & lastrow).Value = "0"
wsUp.Range("K2", "k" & lastrow).Value = sDate
wsUp.Range("I:J").NumberFormat = "0.00"
wsUp.Range("S:T").NumberFormat = "0.00"

For iRow2 = 2 To lastrow
If Cells(iRow2, 9) = "" Then
    wsUp.Cells(iRow2, 9).Value = "0.00"
    wsUp.Cells(iRow2, 19).Value = "0.00"
    ElseIf Cells(iRow2, 10) = "" Then
    wsUp.Cells(iRow2, 10).Value = "0.00"
    wsUp.Cells(iRow2, 20).Value = "0.00"
End If
Next iRow2

wsInst.Activate



End Sub

1 个答案:

答案 0 :(得分:0)

代码会跳过您的if阻止,因为未提及该单元格的父级,因此它使用activesheet,您必须明确提及,而不是

If Cells(iRow2, 1) = "CCC $" Then

这一行:

MySheet.Cells(iRow2, 1) = "CCC $" Then

我不知道代码中哪一张是MySheet,所以请自行替换