我要瘋了,試圖弄清楚這個相對簡單的事情。如何在“For Each”行回圈中提取屬性?
這是我的代碼的一個版本
Dim wb as Workbook
Dim ws as Worksheet
Dim curr_region as range
set wb = ThisWoorkbook
set ws = wb.Worksheets(1)
set curr_region = ws.Range("A1").CurrentRegion 'this range is a 9 columns and 24 rows
For each curr_row in curr_region.Rows
curr_row(1,2).Value2 ''''error occurs here
Next
回圈一直運行,直到我嘗試從 curr_row 中獲取值并且我不明白出了什么問題。在本地視窗中,我可以看到擴展為進一步擴展為的"Value2"屬性(這是正確的術語嗎?)"Value2(1)"Value2(1,1)....Value2(1,9)
Value2(1,2)當它出現在本地視窗中時,我該如何訪問?我嘗試了以下方法:
curr_row(1,2).Value
curr_Row(1,2).Value2
curr_row.Value2(1,2)
curr_row.Value(1,2)
curr_row(1,2)
curr_row(1)
不用說我要瘋了,因為這些都不起作用,我在這里做錯了什么?除了這個特定示例之外,獲取物件屬性的一般語法是什么?我以為一旦我能在當地看到它,這將是一個簡單的映射,但我很困惑。簡直把我逼瘋了。
uj5u.com熱心網友回復:
在這里使用似乎很簡單Cells:
curr_row.Cells(1, 2).Value2
例如:
Dim foo As Variant
foo = curr_row.Cells(1, 2).Value2
Debug.Print curr_row.Cells(1, 2).Value2
curr_row.Cells(1, 2).Value2 = "bar"
最佳實踐是宣告所有變數:
Dim curr_row As Range
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/518021.html
標籤:擅长vba
