Excel VBA / Sheets.Add.Name循环抛出错误9

时间:2016-03-31 09:55:02

标签: excel vba excel-vba

我是Excel VBA的新手,并开始构建用于学习的时间跟踪工作簿。 其中一部分是For-Loop,它将为一年中的每个月添加一个命名工作表:

Sub newyear()

Dim month(12) As String
Dim i As Integer

month(1) = "Januar"
month(2) = "Februar"
...
month(12) = "Dezember"

For i = 1 To 12
On Error Resume Next
Sheets.Add(Tabelle1).Name = month(i)
MsgBox Err.Number    <- this throws Error 9: "Subscript Out Of Range" after
                        every worksheet added during the loop
Next i

End Sub

在运行期间,当循环在工作表之后添加工作表时,MsgBox会在每个添加错误9的单个工作表后弹出:&#34;下标超出范围&#34;。

我不知道为什么会发生这种情况,开始在网上阅读相当多但仍然没有解决方案......也许我错过了一些基本的东西,因为我是初学者。< / p>

请帮帮我。

提前致谢!

2 个答案:

答案 0 :(得分:2)

请参阅以下代码。

Sub newyear()
On Error Resume Next
Dim month(12) As String
Dim i As Integer
month(1) = "January"
month(2) = "February"
month(3) = "March"
month(4) = "April"
month(5) = "May"
month(6) = "June"
month(7) = "July"
month(8) = "August"
month(9) = "September"
month(10) = "October"
month(11) = "November"
month(12) = "Dezember"
Dim ws As Worksheet
For i = 1 To 12
    With ThisWorkbook
        If Worksheets(month(i)).Name <> month(i) Then
            Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
            ws.Name = month(i)
        End If
    End With
    If Err.Description <> "" Then
        Err.Clear
        'do what you want to do 
    End If
Next i
End Sub

答案 1 :(得分:0)

我不确定您为什么要避免出错。你可以为它制作一个longwinded代码,但没有用。你也可以尝试更短的东西:

func processVisibleItems(){
    let visibleItems = self.collectionView?.indexPathsForVisibleItems()
    for p in visibleItems! as [NSIndexPath]{
        // create a layer which will contain the cell for each of the visibleItems
        let containerLayer = CALayer()
        containerLayer.frame = (self.collectionView?.layer.bounds)!
        containerLayer.backgroundColor = UIColor.clearColor().CGColor

        // we need to change the anchor point which will offset the layer - adjust accordingly
        var containerFrame:CGRect! = containerLayer.frame
        containerFrame.origin.x -= containerLayer.frame.size.width/2
        containerLayer.frame = containerFrame
        containerLayer.anchorPoint = CGPointMake(0.0, 0.5)
        self.collectionView?.layer.addSublayer(containerLayer)

        //add the cell to the new layer 
        let cell = self.collectionView?.cellForItemAtIndexPath(p) as! NewspaperImageCollectionViewCell
        cell.frame = containerLayer .convertRect(cell.frame, fromLayer: cell.superview!.layer)
        containerLayer.addSublayer(cell.layer)
        addAnimationForLayer(containerLayer)
     }
}

func addAnimationForLayer(layerToAnimate: CALayer){
    // fade-out animation
    let fadeOutAnimation = CABasicAnimation(keyPath: "opacity")
    fadeOutAnimation.toValue = 0.0

    //rotation Animation
    let rotationAnimation = CABasicAnimation(keyPath: "transform")
    var tfm = CATransform3D()
    tfm = CATransform3DMakeRotation((65.0 * CGFloat(M_PI) )/180.0, 0.0, -1.0, 0.0)
    tfm.m14 = -0.002
    rotationAnimation.fromValue = NSValue(CATransform3D:CATransform3DIdentity)
    rotationAnimation.toValue = NSValue(CATransform3D:tfm)

    //group the animations and add to the new layer
    let group = CAAnimationGroup()
    group.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    group.fillMode = kCAFillModeForwards
    group.removedOnCompletion = false
    group.duration = 0.35
    group.animations = [rotationAnimation,fadeOutAnimation]
    layerToAnimate.addAnimation(group, forKey: "rotateAndFadeAnimation")
 }