2014-10-05 49 views
-2
Sub arraystring() 
    Dim strname() As String, count As Integer 
    Dim element As Variant 
    count = 0 
    strname = ("sathesh") 
    For Each element In strname 
    count = count + 1 
    Next 
    MsgBox "The total Count is" & count 
End Sub 

有人可以帮助我在上面的代码。我需要获得数组字符串中分配的字符串的计数。数组字符串的vba宏

+0

您为strname分配了一个字符串“sathesh”,但strname被声明为字符串数组。这应该首先得到纠正。在将字符串分配给strname之前,您永远无法获得字符串的数量。 – 2014-10-05 02:49:48

+1

你只是在寻找'Len(“string”)' – Matt 2014-10-05 03:23:32

回答

0

如果您需要声明未知维数的字符串数组,可能应该放弃字符串数组(以及后续的ReDim命令),以支持变体数组。

Sub arraystring() Dim strname As Variant, count As Long strname = Array("sathesh", "hsehtas") count = UBound(strname) + 1 'a variant array is zero based MsgBox "The total Count is " & count End Sub

的变体基于阵列的,所以你需要1添加到上边界(即计数)是零。