gpt4 book ai didi

excel - 查找列然后过滤

转载 作者:行者123 更新时间:2023-12-04 22:18:43 26 4
gpt4 key购买 nike

我的标题从 A3 开始,我试图让这段代码查找“团队经理”列,然后过滤登录名。
这是我的代码,但它无法正常工作。

Sub FilterNames()

Dim col As String, cfind As Range

col = "Team Manager"
With ActiveSheet.Select
With .Range("A3", .Cells(1, .Columns.Count).End(xlToLeft))
Set cfind = .Find(what:=col, LookIn:=xlValues, lookat:=xlWhole)
If Not cfind Is Nothing Then
.AutoFilter Field:=cfind.Column, Criteria1:="login123" '
End If
End With
.AutoFilterMode = False
End With

End Sub

最佳答案

这是一个有趣的小问题。请尝试以下代码,根据您在第 3 行中的所有标题,在名为 Sheet1 的工作表上- 更改这些以适应。

Option Explicit
Sub FilterNames()
Dim ws As Worksheet
Dim LastRow As Long, col As Long

Const login = "login123"
Const header = "T*M*"

Set ws = Sheets("Sheet1")
col = Application.WorksheetFunction.Match(header, ws.Range("3:3"), 0)
LastRow = ws.Cells(Rows.Count, col).End(xlUp).Row

With ws.Range(ws.Cells(3, col), ws.Cells(LastRow, col))
.AutoFilter 1, login
End With

End Sub

关于excel - 查找列然后过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66377431/

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