2012-09-24 141 views
0

在使用VBA for Excel時,我遇到了字符串數據類型的運行時錯誤。VBA Excel字符串

我有一個變量聲明爲字符串,並嘗試從電子表格中的單元格中讀取一個值,並添加爲列表框中的項目。存在於特定單元格中的數據大於2000個字符。我的vba能夠讀取單元格中的值並在調試窗口中打印。 當我嘗試在列表框中添加項目時,它無法添加。它會拋出運行時錯誤

運行時錯誤代碼是「-2147352571(80020005):類型不匹配」

有沒有解決這類問題的方法。

Public Sub update_form() 
Dim a1, b1, c1, d1 As Single 
Dim a2, b2, c2, d2 As String 
Dim a3, b3, c3, d3 As String 
Dim a4, c4, d4 As String 
Dim i As Single 
Dim b4$ 

    a2 = req_no.Value 
    Sheets("Design Trace - Current").Select 
     Range("A1").Activate 
     Columns("A:A").Select 
     Selection.Find(What:=a2, After:=ActiveCell, LookIn:=xlFormulas, _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False, SearchFormat:=False).Activate 
     a1 = ActiveCell.Row 
    b2 = Sheets("Design Trace - Current").Cells(a1, 2).Value 

    c2 = "A" & a1 
    d2 = "BBB" & a1 
    b1 = Application.WorksheetFunction.CountA(Range(c2, d2)) 
    For i = 2 To b1 Step 3 
     a4 = Cells(a1, i).Value 
     b4 = Cells(a1, i + 1).Value 
     d1 = Len(b4) 
     Debug.Print " Length : " & d1 
     c4 = Cells(a1, i + 2).Value 

     design_ele.Text = a4 
     reverse_req.Text = b4 
     code_file_name.Text = c4 
     Debug.Print "a4 : " & a4 
     Debug.Print "b4 : " & b4 
     Debug.Print "c4 : " & c4 

     If (Len(a4) > 500) Then 
      ListBox1.AddItem "Refer Value" 
     Else 
      ListBox1.AddItem a4 
     End If 

     ListBox2.AddItem b4 

     If (Len(c4) > 500) Then 
      ListBox3.AddItem "Refer Value" 
     Else 
      ListBox3.AddItem c4 
     End If 


    Next 
End Sub 

這是一個用戶窗體和Excel的版本是2007年

感謝

+0

是否需要在列表框中顯示2000個字符的項目? – shahkalpesh

+0

最好有2000個字符... – Matt

+0

不,我已經聲明瞭所有我使用的變量。 – Matt

回答

1

有個〜2000個字符的限制,以一個單獨的列表框項目。

如果你想避免錯誤,限制長度增加時:

listbox3.additem left$(a4, 2000) 

如果你想存儲的全文在數組中這樣做。

+0

感謝它的工作... – Matt