下面的第一個子程式不起作用,但第二個子程式起作用。為什么?
兩者之間的唯一區別是 rs 變數的型別(集合與變體)。
第一個子例程給了我以下錯誤:
“運行時‘13’:型別不匹配”
Sub It_Doesnt_Work()
Dim rg As Excel.Range, rs As Collection
Set rg = Application.Range("myRange")
Set rs = rg.CurrentRegion.Rows
Debug.Print rs.Count
End Sub
Sub It_Works()
Dim rg As Excel.Range, rs As Variant
Set rg = Application.Range("myRange")
Set rs = rg.CurrentRegion.Rows
Debug.Print rs.Count
End Sub
uj5u.com熱心網友回復:
這rg.CurrentRegion.Rows是型別Range,您嘗試將其推送到rs As Collection哪個集合中。由于 Excel 沒有將范圍隱式轉換為集合,因此您不能這樣做。
要么您撰寫一個顯式轉換這些型別的函式,要么您需要定義您的變數rs As Range或Variant像您所做的那樣,但Range會更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/460538.html
