从模块返回值

时间:2019-03-08 14:07:30

标签: vb.net

我似乎无法在例程结束时返回计数器值。

Sub CountCheckBoxes(sldTemp As Slide)
    Dim counter As Integer
    Dim shpTemp As Shape
    For Each shpTemp In sldTemp.Shapes
        If shpTemp.Type = msoOLEControlObject Then
            If TypeName(shpTemp.OLEFormat.Object) = "CheckBox" Then
                If shpTemp.OLEFormat.Object.Value = True Then
                    counter = counter + 1             
                End If
            End If
        End If
    Next
    Return counter
End Function

编辑 新代码:问题是当我键入Return Counter然后按Enter键。我具有此功能的原因是计算一张幻灯片上有多少个复选框为true,然后返回值:

Function CountCheckBoxes(sldTemp As Slide) As Integer
    Dim counter As Integer
    Dim shpTemp As Shape

    For Each shpTemp In sldTemp.Shapes
        If shpTemp.Type = msoOLEControlObject Then
           If TypeName(shpTemp.OLEFormat.Object) = "CheckBox" Then
              If shpTemp.OLEFormat.Object.Value = True Then
                 counter = counter + 1
              End If
           End If
        End If
    Next
    Return counter 
End Function

1 个答案:

答案 0 :(得分:2)

为了娱乐:

Function CountCheckBoxes(sldTemp As Slide) As Integer
    Return sldTemp.Shapes.Count(Function(s) s.Type = msoOLEControlObject AndAlso TypeName(s.OLEFormat.Object) = "CheckBox" AndAlso s.OLEFormat.Object.Value)
End Function