2014-10-05 66 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添加到上邊界(即計數)是零。