在循环中调用sub

时间:2015-07-10 03:14:31

标签: vba

试图从另一个子(Macro2)内部调用sub(trialMacro)但不知何故它没有被激活。有原因吗?如果调用函数不在循环内,则它可以正常工作。即如果我只是在另一个sub下做一个普通的调用函数。我的编码是对的吗?我不确定循环是否会中断执行

  ' trial Macro
Sub trialMacro()
Dim PrevCell As Range
Set PrevCell = ActiveCell
    SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:="0", ByChange:= _
       "$M$2,$M$3,$M$5,$M$7"
   SolverSolve UserFinish:=True
    SolverFinish KeepFinal:=1
    'Copy in sample and out of sample error
    PrevCell.Resize(1, 3).Copy

    'Paste Values of in sample and out of sample errors
    PrevCell.Offset(0, 4).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    'Copy Co-efficient
    Range("M2:M7").Select
    Application.CutCopyMode = False
    Selection.Copy

    'Select paste destination
    PrevCell.Offset(0, 7).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True

    'Copy Paste Following months data
    PrevCell.Offset(1, -1).Resize(12, 1).Copy

    'Select target destination
    PrevCell.Offset(0, 13).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
PrevCell.Offset(1, 0).Select
End Sub

Sub Macro2()
'
' Macro2 Macro
    Dim i, j As Integer

    For i = 50 To 162
    For j = 0 To 113
    Sheets("Model v2 DUBDAT >0").Cells(i, 17).Select
    Call trialMacro
    Range("P50:BA50").Offset(j, 0).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Reference Sheet").Select
    Range("D6").Offset(j, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Model v2 DUBDAT >0").Select
    Next
    Next
End Sub

1 个答案:

答案 0 :(得分:-1)

给这个读......

https://msdn.microsoft.com/en-us/library/office/gg251432.aspx

从此行中删除电话

Call trialMacro

应该是..

trialMacro

如果使用call语句,则子名必须后跟括号。