Optional ranges in vba function

后端 未结 2 606
执念已碎
执念已碎 2021-01-18 18:54

I am trying to return the columns count on a range, sometimes I need one range, but, sometimes I need more than one range.

I have put in optional ranges so I can cho

2条回答
  •  一个人的身影
    2021-01-18 19:39

    Try it like this

    Function GetColoumnCount(ARange1 As Range, Optional ARange2 As Range, Optional ARange3 As Range, Optional ARange4 As Range) As Long  
        Dim Result As Long
        Result = 0
    
        Result = ARange1.Columns.Count ' This works
        If Not ARange2 Is Nothing Then
            Result = Result + ARange2.Columns.Count
        End If
    
        GetColoumnCount = Result    
    End Function
    

提交回复
热议问题