2017-08-25 40 views
0

主要目標是:使用列引用名稱從篩選表中的每一行中檢索特定的單元格值。VBA:從Range.Area中的每一行中檢索單元值

到目前爲止,我有以下的代碼

Dim table As listObject 
Dim columns As ListColumns 
Dim row As ListRow 
Dim rnData As range 
Dim rngArea As range 

Set table = Sheets(sheetName).ListObjects(TableName) 
Set columns = table.ListColumns 
Set rnData = ThisWorkbook.Worksheets(sheetName).ListObjects(TableName).range 
'Notice that sheetName and TableName are function arguments. No need to pay attention. Consider any string values. 

'Filter my table 
table.range.AutoFilter Field:=7, Criteria1:=Array("filtervalue1", "filtervalue2"), Operator:=xlFilterValues 

'Set the filtered table in a new Range object 
Set rnData = ThisWorkbook.Worksheets(sheetName).ListObjects(TableName).range 

'Count all rows of my filtered table 
With rnData 
    For Each rngArea In .SpecialCells(xlCellTypeVisible).Areas 
     lCount = lCount + rngArea.Rows.Count 
    Next 
End with 

現在我想我的循環過濾表(我的「rnData」範圍),我想在這些Range.Areas各行的單元格值。

我在想這樣的事情,但我在使用VBA困難做到這一點:

For iRowNo = 2 To (lCount - 1) 'Start at 2 because 1 is the table header 
      'This does not work once it gets another row from the entire table. Not the filtered one. Help here! 
      Set row = table.ListRows(iRowNo) 

      'Something close to this - Help Here! 
      Set row = rnData.SpecialCells(xlCellTypeVisible).Areas 

      ''Would like to have the code like this to get the values 
      cell1Value= row.range(1, columns("My Column Header 1").Index).value 
      cell2Value= row.range(1, columns("My Column Header 2").Index).Value 

Next iRowNo 

讓我知道,如果有不同的解決方案莫過於此。

+1

爲什麼不裏面的'對於每個rngArea ...'循環運行另一個'對於每一個[變量]在rngArea.rows'循環?這樣你就可以得到所有的行(只要記住你指的是整行的限制,所以如果你想要到一個單元格,你需要'[variable] .cells(1,y)') –

+0

不清楚(對不起我英語不好),你能添加你有什麼和你想要的圖片嗎?我想,對其他人來說也會更容易。 – AntiDrondert

+0

@DirkReichel謝謝你帶來了!解決了這個問題。我會回答我自己的問題。 –

回答

1

繼@DirkReichel答案

這裏是爲我工作的代碼:

Dim table As listObject 
Dim columns As ListColumns 
Dim row As ListRow 
Dim rnData As range 
Dim rngArea As range 

Set table = Sheets(sheetName).ListObjects(TableName) 
Set columns = table.ListColumns 
Set rnData = ThisWorkbook.Worksheets(sheetName).ListObjects(TableName).range 
'Notice that sheetName and TableName are function arguments. No need to pay attention. Consider any string values. 

'Filter my table 
table.range.AutoFilter Field:=7, Criteria1:=Array("filtervalue1", "filtervalue2"), Operator:=xlFilterValues 

'Set the filtered table in a new Range object 
Set rnData = ThisWorkbook.Worksheets(sheetName).ListObjects(TableName).range 

'Get values for each row 
With rnData 
    For Each rngArea In .SpecialCells(xlCellTypeVisible).Areas 
     For Each row In rngArea.Rows 
      cell1Value= row.range(1, columns("My Column Header 1").Index).value 
      cell2Value= row.range(1, columns("My Column Header 2").Index).Value 
     Next 

     'lCount = lCount + rngArea.Rows.Count 'Removed this. 
    Next 
End with 

'Also no need the second part of code with the For..Next loop. 
1

我認爲你是間接嘗試創建一個數組,這不是一個可以輕易解釋的單個帖子,但是這裏有一些代碼可以幫助你開始。

我打算假設你設置的rnData範圍是正確的。從那裏開始,循環遍歷範圍內的所有單元格可能是最簡單的。你可以編寫比下面更精確的代碼,但這應該會幫助你看到除了你想要的東西之外的一些想法。

最重要的是我認爲你正在尋找一種方法來創建一個數組。我希望這有幫助。

Sub testCoutinho() 
Dim Rcell As Range 
Dim rnData As Range 'you'll have to set this up... 

Dim YesLetsDoAnArray As Boolean: YesLetsDoAnArray = False 'or change to false to just make a new sheet with values 

If YesLetsDoAnArray Then 
    ReDim This_is_your_Array(0) As Variant 'Create Array 
    Dim x As Integer 
Else 
    'putting values on a new worksheet in file 
    Dim CleanWS As Worksheet: Set CleanWS = ThisWorkbook.Sheets.Add 
End If 


For Each Rcell In rnData.Cells 

    If Rcell.EntireRow.Hidden = False Then 

     If YesLetsDoAnArray Then 
      ReDim Preserve This_is_your_Array(x) 
      This_is_your_Array(x) = Rcell.Value 
      x = x + 1 
     Else 
      CleanWS.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = Rcell.Value 
     End If 
    End If 
Next Rcell 


'If you used an array, you'll know have variable(s) that contain all your data. 
'your first one is This This_Is_Your_Array(0), followed by This_Is_Your_Array(1)... etc. 
'you can play around. this will print them all. 

If YesLetsDoAnArray Then 
Dim i As Integer 

For i = 0 To x - 1 
Debug.Print This_is_your_Array(i) 
Next i 
End If 


End Sub 
+0

謝謝,這可能是一個選項,但我想保持與我的代碼類似,並使用listobject數據類型。 –

相關問題