2015-10-18 152 views
0

如何正確轉置下列數組?我運行代碼時出現「超出範圍」錯誤。值應該從B2開始填充。轉置陣列運行時錯誤'9'

Sub Checkb() 
    Dim acat As Variant 
    ReDim acat(0) 
    Dim contr As Control 

    For Each contr In UserForm2.Controls 
     If TypeName(contr) = "CheckBox" Then 
      If contr.Value = True Then 
       MsgBox (contr.Value) 
       acat(UBound(acat)) = contr.Name 
       ReDim Preserve acat(UBound(acat) + 1) 
      End If 
     End If 
    Next 

    ReDim Preserve acat(UBound(acat) - 1) 

    Sheets("TBL").Select 
    Range("B2").Resize(UBound(acat, 2), UBound(acat, 1)) = Application.Transpose(acat) 
End Sub 
+0

'acat'是一維的。最後一行將其視爲二維。 – GSerg

回答

1

更改最後一行是:

Range("B2").Resize(UBound(acat)) = Application.Transpose(acat)