Vba 宏观解释
我正在研究如何在VBA的帮助下创建快捷方式。宏基本上允许我根据单元格的值过滤范围。我在网上找到了这个宏Vba 宏观解释,vba,excel,Vba,Excel,我正在研究如何在VBA的帮助下创建快捷方式。宏基本上允许我根据单元格的值过滤范围。我在网上找到了这个宏 Public Sub FilterOnCellValue() Dim nField As Long With ActiveCell nField = .Column - .CurrentRegion.Cells(1).Column + 1 .CurrentRegion.AutoFilter Field:=nFie
Public Sub FilterOnCellValue()
Dim nField As Long
With ActiveCell
nField = .Column - .CurrentRegion.Cells(1).Column + 1
.CurrentRegion.AutoFilter Field:=nField, Criteria1:=.Value
End With
End Sub
代码运行良好,但我很难理解这家伙是如何创建的。到目前为止,我所知道的是,这些人正在创建一个变量。但由于某些原因,变量不是字符串格式,而是“long”,当我基于单元格中的文本进行筛选时,代码工作得非常好。我认为long只能用于数字,如果我错了,请纠正我(我认为我错了:)。其次.column-.currentregion怎么了。单元格….+1
?我真的不知道他在那里干了什么
我真的很感谢你的帮助。到目前为止,这个论坛对我非常有用。了解任何事物的秘诀就是将其分解成若干部分 不管怎样,这些州
Public Sub FilterOnCellValue()
Dim nField As Long
With ActiveCell
'.Column: Column Number i.e A=1,B=2 etc
'.CurrentRegion.Cells(1).Column: Gets the col No of the firstcell of the data region
nField = .Column - .CurrentRegion.Cells(1).Column+1
'Autofilter takes column number as the parameter
'see example below
'nField: gets the col number of the filter value within the region
' leftmost field is 1 hence +1
.CurrentRegion.AutoFilter Field:=nField, Criteria1:=.Value
End With
End Sub
例如,如果您的数据采用这种格式
A B
1 ID Value
2 1 Apple
3 2 Orange
4 3 Banana
5 4 Apple
6 5 Banana
假设您单击单元格B5(苹果),然后运行宏