使用过滤器选项移动数据输入

时间:2018-02-07 13:57:33

标签: excel vba excel-vba

首先准备长时间阅读,我尽可能地缩小信息范围。

所以我现在使用VBA一段时间了,而且我遇到了一个我似乎无法解决的问题。我在VBA中使用代码执行以下操作: Sheet" Two"包含多行数据集。可以使用工作表第一行中的下拉菜单过滤这些数据集。我已经设置了一个宏来检查是否存在必要的数据集,可以通过查看某些行中是否存在文本来检查。如果有文本,则相应的框被填充并相应地着色,如果缺少数据则同样适用。循环完成后,它会计算包含所有数据和缺失数据的单元格摘要,并在Sheet" One"。

的概述中填写。

当代码需要为整个列运行以检查数据时,它会毫无问题地执行此操作。但是,当选择过滤器时,代码不会从过滤的选择开始,而是从列中的第二个单元格开始。这样做是因为我将第二个单元格定义为起点。然而,我无法弄清楚如何以一种动态的方式定义起点,它将遵循过滤器设置。此外,代码不会补偿"间隙" (例如,当过滤器设置使行从第5行跳到第30行时,代码将从5开始依次计数,它不会一直跳转)。由于数据集的数量可以达到150.000左右,因此可能存在很多差距,因此这会大大削弱代码。 请参阅下面的代码。

    Sub CompletionStatusUpdate()

    Dim CompletionStatus As Range 'Creates list to check for Completion Status
    Dim DataGetCompletion As Long 'Defines counter to determine maximum list limit to check

    ThisWorkbook.Sheets("Two").Range("H2:H" & ThisWorkbook.Sheets("One").Range("H9").Value).Interior.ColorIndex = 0 'Reset Colors in 2nd Tab for Completion Status
    ThisWorkbook.Sheets("Two").Range("H2:H" & ThisWorkbook.Sheets("One").Range("H9")).Value = "" 'Reset Values in 2nd Tab for Completion Status  
    'Cell H9 in Sheet One contains a CountA function which checks the amount of data present in Sheet Two. Right now it counts the entire amount of data in row C, 
    'however this needs to be adjusted to only count the cells of data which are filtered in row C

    For Each CompletionStatus In ThisWorkbook.Sheets("Two").Range("H2:H" & ThisWorkbook.Sheets("One").Range("H9").Value + 1) 
    'Creates loop for cells that need to be filled/colored
    'This needs to run over only the filtered cells in the selection
    'Instead of over the H column untill H9 value is reached regardles of filters

    DataGetCompletion = (DataGetCompletion + 1) 'DataGetCompletion Counter for Range, used to move the position of cells to fill in

    ThisWorkbook.Sheets("One").Range("H6").Value = DataGetCompletion + 2 'Ticks up for each loop run through, corrected for the starting cell
    'Again this needs adjust dependant on the filter settings

    If ThisWorkbook.Sheets("Two").Range("D" & ThisWorkbook.Sheets("One").Range("H9")).Value = "Yes" And _
       ThisWorkbook.Sheets("Two").Range("F" & ThisWorkbook.Sheets("One").Range("H9")).Value = "Yes" Then 
       ThisWorkbook.Sheets("One").Range("H8").Value = 1 'Both Data sets are present,used in separate logic
    End If 'This needs to check only filtered cells as well, instead of all cells

    If ThisWorkbook.Sheets("Two").Range("D" & ThisWorkbook.Sheets("One").Range("H9")).Value = "No" And _
       ThisWorkbook.Sheets("Two").Range("F" & ThisWorkbook.Sheets("One").Range("H9")).Value = "No" Then 
       ThisWorkbook.Sheets("One").Range("H8").Value = 0 'Both data sets missing, used in separate logic
    End If 'This needs to check only filtered cells as well, instead of all cells

    If ThisWorkbook.Sheets("One").Range("H8") = 0 Then 'Both data sets missing, so problem
       CompletionStatus.Interior.ColorIndex = 3 'Colors cell red
       CompletionStatus.Value = "Both data sets missing" 'Displays missing information
    End If

    If ThisWorkbook.Sheets("One").Range("H8") = 1 Then 'Data sets complete
       CompletionStatus.Interior.ColorIndex = 4 'Colors cell green
       CompletionStatus.Value = "Both data sets complete" 'Displays completion
    End If

    Next CompletionStatus 'Reruns loop till completion

    ThisWorkbook.Sheets("One").Range("H11").Value = Application.WorksheetFunction.CountIf _
    (ThisWorkbook.Sheets("Two").Range("H2:H" & ThisWorkbook.Sheets("One").Range("H9").Value + 1), "Both data sets complete")

    'Displays amount of complete data sets 
    'This part of the code also needs to run over the filtered selection in the H column, instead of starting from H2 and running till value Sheet One, H9 is reached

    ThisWorkbook.Sheets("One").Range("H13").Value = Application.WorksheetFunction.CountIf _
    (ThisWorkbook.Sheets("Two").Range("H2:H" & ThisWorkbook.Sheets("One").Range("H9").Value + 1), "Both data sets missing")

    'Displays amount of missing data sets
    'This part of the code also needs to run over the filtered selection in the H column, instead of starting from H2 and running till value Sheet One, H9 is reached

     End Sub

我似乎无法使用过滤器,我尝试了.SpecialCells(xlCellTypeVisible)代码的不同应用程序,但它并没有帮助我。

任何帮助将不胜感激,如果不清楚,请告诉我。

1 个答案:

答案 0 :(得分:1)

也许尝试检查行是否在循环范围内可见。原则是,无论您循环播放,请查看.EntireRow.Hidden的{​​{1}}状态是否为CompletionStatus。如果False表示它可见且您想要进行检查。

False

我重写了整个代码,但不知道你的数据是什么样的,如下所示。我不希望它为你起作用,但会向你展示一个结构。我不认为很多元素实际上正在做任何事情,这就是我删除它们的原因。让我们不要错。但是,一开始就解决问题的原则仍然是一样的。

 For Each CompletionStatus In loopRange

        If CompletionStatus.EntireRow.Hidden = False Then

            Select Case h8Range

            Case 1 'this was 1 in yours

                CompletionStatus.Interior.ColorIndex = 4
                CompletionStatus.Value = "Both data sets complete"

            Case 2 'this was 0 in yours

                CompletionStatus.Interior.ColorIndex = 3
                CompletionStatus.Value = "Both data sets missing"

            End Select

        End If

    Next CompletionStatus