我想在陣列中使用范圍的內容。它一直有效,直到我在范圍定義中使用 Cells(R,C) 方法為列添加變數。所以我的問題在Range(Cells(headline, ic_from_col), Cells(headline, ic_to_col)) 中是明確的
運行時錯誤 '1004' 應用程式或面向物件的錯誤" <
我錯過了什么?我可以嘗試什么?
Dim export As String: export = "Table1"
Dim headline As Integer: headline = 7
Dim ic_from_col As Integer: ic_from_col = 30
Dim ic_to_col As Integer: ic_to_col = 40
Dim ICNames As Variant: Set ICNames = Sheets(export).Range(Cells(headline, ic_from_col), Cells(headline, ic_to_col))
Dim key As String: key = 1
For Each item In SomethingElse
...
name = ICNames(key)
Next item
(我通過搜索獲得列和行的數字。這是有效的,所以我在這里只放了一些數字。只是為了解釋我為什么要使用變數)。
我按照建議嘗試了以下操作,但得到了相同的結果:
Dim ICNames As Variant
ICNames = Sheets(export).Range(Cells(headline, ic_from_col), Cells(headline, ic_to_col))
讓我展示什么可以解釋我的問題:
Dim export As String: export = "Table"
Dim headline As Long: headline = 7
Dim ic_from_col As Long: ic_from_col = 32
Dim ic_to_col As Long: ic_to_col = 42
Dim ICNames As Variant: Set ICNames = Sheets(export).Range("AF" & headline & ": AQ" & headline)
' Dim ICNames As Variant: Set ICNames = Sheets(export).Range(Cells(headline, ic_from_col), Cells(headline, ic_to_col))
Dim key As String: key = 1
Name = ICNames(key)
MsgBox (Name)
uj5u.com熱心網友回復:
所以,這就是我為我的案例解決的方法。我用過“.address”:
ICNames = Sheets(export).Range(Cells(headline, ic_from_col).Address & ":" & Cells(headline, ic_to_col).Address)
uj5u.com熱心網友回復:
如果作業表名稱正確,則需要洗掉 Set keywork,因為它不是物件。
ICNames = Sheets(export).Range(Cells(headline, ic_from_col), Cells(headline, ic_to_col))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/345272.html
上一篇:如何使用陣列作為范圍?
下一篇:使用換行符加載CSV
