【发布时间】:2014-12-08 17:30:07
【问题描述】:
我正在尝试创建一个函数来根据插入的匹配字符串获取单元格的列号。如果在第一行找到两个匹配项,我想返回最后一个匹配项。例如,“TotalSalary Jan”和“TotalSalary Feb”。将“TotalSalary”作为参数插入后,我将获得“TotalSalary Feb”的列号。我的代码:
Private Function GetColumnNumber(name As String) As Integer
Dim res As Object, ret As Integer
Set res = Sheets("Unified").Cells(1, 1).EntireRow.Find(What:=name, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Not res Is Nothing Then
ret = res.Column
Do
Set res = .FindNext(res)
ret = res.Column
Loop While Not res Is Nothing And res.Column <> ret
GetColumnNumber = ret
End If
End Function
顺便说一句,代码运行不正常。 res 对象找不到列号的下一个。
【问题讨论】: