gpt4 book ai didi

excel - 如何将变量传递到范围? (VBA)

转载 作者:行者123 更新时间:2023-12-02 11:49:29 25 4
gpt4 key购买 nike

我正在创建一个宏 (Excel)

我通过变量保存工作表上使用的行数,我需要将其传递到过滤器范围,希望这段代码有助于解释我想要做什么...

Sub Filtering()

Rows("11:11").Select
Selection.AutoFilter

'save the amount of rows used on the sheet
Dim lastRow As Long
lastRow = Range("AC" & Rows.Count).End(xlUp).Row

ActiveSheet.Range("$A$11:$AC$lastRow").AutoFilter Field:=18, Criteria1:=">10", _
Operator:=xlAnd

End Sub

过滤器将始终位于第 11 行,列将始终从 A 到 AC,但每个 Excel 文件的行数不同。你们能帮忙吗?请

最佳答案

你们很接近。与您为最后一行定义范围时的方法类似。该变量必须位于 ""

之外
Option Explicit

Sub Filtering()

'Rows("11:11").Select 'Redudant
'Selection.AutoFilter 'Redudant

'save the amount of rows used on the sheet
Dim lastRow As Long
lastRow = Range("AC" & Rows.Count).End(xlUp).Row

ActiveSheet.Range("$A$11:$AC" & lastRow).AutoFilter Field:=18, Criteria1:=">10", _
Operator:=xlAnd

End Sub

结果

enter image description here

关于excel - 如何将变量传递到范围? (VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53144889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com