简体   繁体   English

我可以在VBA Excel中设置动态范围以在过滤器中使用吗

[英]Can I set a dynamic range in vba excel to use in filter

The following excel sub is a filter that is filtering out rows based on the rows in the criteria row. 以下excel子项是一个过滤器,用于根据条件行中的行过滤出行。 The code works well when the ranges are set with absolute data. 当使用绝对数据设置范围时,该代码可以很好地工作。 I want to change the code to take the range from references stored as cell values (an indirect reference) but I cannot find a way to adapt other code snippets I see to work and I wonder if anyone can help me. 我想更改代码以从存储为单元格值的引用(间接引用)中获取范围,但是我找不到一种方法来适应我认为有效的其他代码段,并且我想知道是否有人可以帮助我。 I am not a programmer. 我不是程序员。

The problem is that as new data is inserted from time to time the range reference will move and the start of the data an the associated filter is in cell (using RC notation 14,14) and the data in cell 13,12. 问题在于,随着不时插入新数据,范围参考将移动并且数据的开始以及相关联的过滤器位于单元格中(使用RC标记14,14)以及单元格13,12中的数据。 While I know I can't use the indirect function in vba I wondered if there is a way to dynamically assign a range to be able to use the Advance filter function. 虽然我知道我无法在vba中使用间接函数,但我想知道是否有一种方法可以动态分配范围以使用高级过滤器功能。 I have the code to find the last column and row of the data block. 我有找到数据块的最后一列和一行的代码。 I have tried the following code (2 attempts) but it won't let me use the object in this way I have tried to crate the cell reference as a string then assign it using the range function. 我已经尝试了以下代码(两次尝试),但是它不允许我以此方式使用对象,而是尝试将单元格引用创建为字符串,然后使用range函数进行分配。 I then read an answer where someone had put the value of the cells directly into the range function and it has worked for them ( they were copying cells). 然后,我读了一个答案,有人将单元格的值直接放入range函数中,并且对他们有用(他们正在复制单元格)。 The 2 attempt are broadly the same but in the second I am trying to be more specific. 两次尝试大致相同,但是第二次尝试更加具体。 The issue seems to be as soon as I change from an absolute reference "A50" in the range statement the range no longer works. 问题似乎是我从range语句中的绝对引用“ A50”更改后,该范围不再起作用。 I am unsure how to resolve this and perhaps it can't be 我不确定如何解决这个问题,也许不可能

It may be helpful to know the that data being filtered is rows of name and telephone data along with a tally system to show attendance (one column per week for a year) The cells with the dynamic data hold them in the form A1 not RC format 了解被过滤的数据是姓名和电话数据的行以及一个显示考勤的提示系统(一年一次,每周一列),可能会有帮助(带有每周动态数据的单元格以A1而非RC格式保存)

Sub UseAdvancedFilterInPlace()
'This version of the sub has absolute references and works perfectly
Dim rdData As Range
Dim rgcriteria As Range
Call TurnOffStuff
Set rgData = Sheet9.Range(“A50”).CurrentRegion
Set rgcriteria = Sheet9.Range(“A46”).CurrentRegion
rgData.AdvancedFilter xlFilterInPlace, rgcriteria
Call TurnOnStuff
End Sub

Sub UseAdvancedFilterInPlace()
'This version of the sub has dynamic references and fails
Dim rdData As Range
Dim rgcriteria As Range
Call TurnOffStuff
Dim Top_of_data As String
Dim Top_of_Criteria As String
Dim My_range As Range
‘Attempt 1
'Set rgData = Range(Sheet9.Cells(13, 12).Value).CurrentRegion
'Set rgcriteria = Range(Sheet9.Cells(14, 14).Value).CurrentRegion
'Attempt 2
Set rgData = Sheet9.Range(Sheet9.Range(Cells(13, 12)).Value).CurrentRegion
Set rgcriteria = Sheet9.Range(Sheet9.Range(Cells(14, 14)).Value).CurrentRegion

rgData.AdvancedFilter xlFilterInPlace, rgcriteria
Call TurnOnStuff

End Sub

The actual error message I get is an application-defined or object-defined error 我收到的实际错误消息是应用程序定义的错误或对象定义的错误

This worked for me. 这对我有用。

Set rdData = Sheet9.Range(Sheet9.Range("L13").Value).CurrentRegion
Set rgcriteria = Sheet9.Range(Sheet9.Range("N15").Value).CurrentRegion

given that Range("L13").Value is A50 and Range("N15").Value is A46. 给定Range("L13").Value为A50和Range("N15").Value为A46。

extra: Use the statement Option Explicit in the first line of every module, out of every sub or function. extra:在每个模块的第一行中,在每个子或函数中使用语句Option Explicit This option throws an error on undeclared variables, and will help you avoid renameing mistakes on variables. 此选项在未声明的变量上引发错误,并且将帮助您避免重命名变量上的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM