我有結構(表名:表2)表象下面這樣:從每行選擇一列在一個表中
使用VBA,我要選擇僅在當前的單獨列的值通過迭代每一行來實現。 下面是代碼,我寫道:
Function findColumnValue(strColCombIdent As String, strColumnName As String) As String
On Error Resume Next
Dim strRetResult As String
Dim wsMapMasterRefSheet As Worksheet
'Referes to the table Table2.
Dim loMapMaster As ListObject
Set wsMapMasterRefSheet = ThisWorkbook.Worksheets("Sheet3")
Set loMapMaster = wsMapMasterRefSheet.ListObjects("Table2")
'All rows of the table Table2
Dim rAllRows As Range
Set rAllRows = loMapMaster.DataBodyRange
'Holds one row from the databody range for processing.
Dim rCurrRow As Range
'Process data
Dim strTemp As String
For Each rCurrRow In rAllRows
strTemp = rCurrRow.Columns(2)
Debug.Print strTemp
Next rCurrRow
findColumnValue = strRetResult
End Function
我希望能得到像下面的結果(列2只值):
1.5
1.5
1.8
4
3
3
1
2
10
12
5
7
相反,我結束了這樣的事(所有從塔#2開始,對於每一個處理的行值。)
1.5
0.045150462962963
1.5
4.52083333333333E-02
1.8
4.72685185185185E-02
4
0.168090277777778
3
3.1
3
8.47800925925926E-02
1
4.16666666666667E-02
2
8.33449074074074E-02
10
10.1.1.1
12
1.3.4.5
5
0.212511574074074
7
8.54166666666667E-02
使用
strTemp = rCurrRow.Columns(1, 2)
代替
strTemp = rCurrRow.Columns(2)
原因的運行時錯誤1004
由於每次迭代指向在一個範圍內的對象的For循環;我正在考慮使用
rCurrRow.Columns(2)
將指向當前行的列#2,因此只打印列的值。 我的邏輯錯位嗎?
一個額外的問題:
爲什麼在MSDN的Excel參考指南描述列的屬性;其中作爲明確的「列」的使用顯然需要參數
這裏是我提到的鏈接: http://msdn.microsoft.com/en-us/library/office/ff197454(v=office.15).aspx
這太神奇了。像魅力一樣工作。只需要注意,當我將For循環內的語句「strTemp = rCurrRow.Range(,2)」更改爲「strTemp = rCurrRow.Range(0,2)」時,它還會打印標題行值「MajorVersion」。你能否請請遮住一些燈光。雖然接受答案。 – Ayusman 2014-09-02 16:32:27
'rCurrRow.Range(,2)'相當於'rCurrRow.Range(1,2)',它指向同一行和第二列。 0表示上面的行。 – Rory 2014-09-02 19:37:45
非常感謝。說得通。 – Ayusman 2014-09-03 04:18:22