热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Firstoffprepareforalongread,IvenarroweddowntheinfoasmuchasIcould.首先准备长时间阅读,我尽可能地缩小

First off prepare for a long read, I've narrowed down the info as much as I could.

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

So I am using VBA since a little while now and I've encountered a problem which I can not seem to solve. I'm using a code in VBA to do the following: Sheet "Two" contains multiple rows of data sets. These data sets can be filtered using dropdown menus in the first row of the sheet. I've set up a macro to check if the necessary data sets are present, which can be checked for by seeing if there is text present in certain rows. If there is text then a corresponding box is filled and colored accordingly, the same applies if data is missing. After the loop is completed it counts a summary of the cells with all data and with missing data an fills this in an overview on Sheet "One".

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

When the code needs to run for the entire column to check data in, it will do so without problems. However when a filter is selected the code does no start at the filtered selection, instead it starts at the second cell in the column. It does because I defined the second cell as the starting point. I cannot figure out however how to define the starting point in such a dynamic way that it will follow with the filter settings. Furthermore the code does not compensate for "gaps" (e.g. when the filter settings make the row jump from row 5 to row 30, the code will keep counting on from 5, it does not jump along so to speak). As the data sets can number up to around 150.000 a lot of gaps can be present, so this cripples the code heavily. See my code below.

当代码需要为整个列运行以检查数据时,它会毫无问题地这样做。但是,当选择过滤器时,代码不会从过滤的选择开始,而是从列中的第二个单元格开始。这样做是因为我将第二个单元格定义为起点。然而,我无法弄清楚如何以一种动态的方式定义起点,它将遵循过滤器设置。此外,代码不补偿“间隙”(例如,当滤波器设置使行从第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

I just cant seem to get it to work with the filters, I've tried different applications of the .SpecialCells(xlCellTypeVisible) code but it didn't help me.

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

Any Help would be greatly appreciated, if something is not clear please let me know.

非常感谢任何帮助,如果不清楚,请告诉我。

1 个解决方案

#1


1  

Maybe try doing a check on whether the row is visible within the loop range. The principle being, whatever you are looping over, see if the .EntireRow.Hidden status of CompletionStatus is False. If False it means it is visible and you want to do your check.

也许尝试检查行是否在循环范围内可见。原则是,无论你循环,查看CompletionStatus的.EntireRow.Hidden状态是否为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

I re-wrote the entire code, without knowing what your data looks like to something like as follows. I don't expect it to work off the bat for you but shows you a structure. I don't think a lot of the elements were actually doing anything which is why I have removed them. Let's hope not in error. However, the principle at the start for how to solve your problem remains the same.

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

Option Explicit

Sub CompletionStatusUpdate()

    Dim CompletionStatus As Range
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim ws1 As Worksheet
    Set ws1 = wb.Sheets("One")
    Dim ws2 As Worksheet
    Set ws2 = wb.Sheets("Two")

    Dim h9Range As Range
    Set h9Range = ws1.Range("H9")

    'Test for h9Range being not empty and that is greater than 2?
    ws2.Range("H2:H" & h9Range.Value + 1).Interior.ColorIndex = 0 'if this is intended to clear prior runs it needs + 1
    ws2.Range("H2:H" & h9Range.Value + 1).ClearContents 'This seems to be the same as loopRange?

    Dim completeRange As Range
    Set completeRange = ws1.Range("H11")

    Dim missingRange As Range
    Set missingRange = ws1.Range("H13")

    missingRange.ClearContents
    completeRange.ClearContents

    Dim h8Range As Range
    Set h8Range = ws1.Range("H8")

    Dim dRange As Range
    Set dRange = ws2.Range("D" & h9Range.Value)

    Dim fRange As Range
    Set fRange = ws2.Range("F" & h9Range.Value)

    Dim countRange As Range
    Set countRange = ws2.Range("H2:H" & h9Range.Value + 1)

    Dim h6Range As Range
    Set h6Range = ws1.Range("H6")

    Dim loopRange As Range

    Set loopRange = ws2.Range("H2:H" & h9Range.Value + 1).SpecialCells(xlCellTypeVisible)

    DataGetCompletion = 3


    If dRange = "Yes" And fRange = "Yes" Then

        h8Range = 1

    ElseIf dRange = "No" And fRange = "No" Then

        h8Range = 2

    Else

        h8Range = 3

    End If


    For Each CompletionStatus In loopRange

        If CompletionStatus.EntireRow.Hidden = False Then

            Select Case h8Range

            Case 1

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

            Case 2

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

            End Select

        End If

    Next CompletionStatus


    completeRange = Application.WorksheetFunction.CountIf _
                    (countRange, "Both data sets complete")

    missingRange = Application.WorksheetFunction.CountIf _
                   (countRange, "Both data sets missing")

End Sub

推荐阅读
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了使用C#实现Word模版打印的方案。包括添加COM引用、新建Word操作类、开启Word进程、加载模版文件等步骤。通过该方案可以实现C#对Word文档的打印功能。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
  • VBA操作Excel之设置单元格属性
    VBA操作Excel简介一、VBA读写Excel文件二、VBA设置单元格属性三、VBA弹出输入和输出窗口参考文档一、VBA读写Excel文件VBA简介及打开Excel文件方法见VB ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
author-avatar
佩刚坤斌冠如_567
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有