目标是在整行(始终为第1行)中查找某个字符串。标识包含字符串的单元格。找到该单元格的列字母(而不是列号)。
我具有从Range中获取列字母的功能,而我又从Stackoverflow的另一页“窃取”了
Function FnColFromRange(rInput As Range) As String
FnColFromRange = Split(rInput.Address, "$")(1)
End Function
然后我有一个子项将Column字母存储到一个名为“ colLetter”的字符串中
Dim colLetter As String
With Workbooks("Wb.xlsx").Worksheets("Ws")
Set myStringCell = .Rows(1).Find(myString)
colLetter = FnColFromRange(myStringCell.Address)
End With
但是它始终停留在“ colLetter”行上,提示“类型不匹配”
答案 0 :(得分:4)
检查.Find()
方法是否返回了实际范围。然后,正如@BruceWayne所评论的那样,错误可能出在myStringCell.Address
:
Dim colLetter As String
With Workbooks("Wb.xlsx").Worksheets("Ws")
If Not .Rows(1).Find(myString) Is Nothing Then
Set myStringCell = .Rows(1).Find(myString)
colLetter = FnColFromRange(myStringCell)
Else
MsgBox "BOO!"
End If
End With
但是,由于myStringCell
有一个名为.Column
的属性,您可能会引用它或使用1衬线代替函数:
Dim colLetter As String
With Workbooks("Wb.xlsx").Worksheets("Ws")
If Not .Rows(1).Find(myString) Is Nothing Then
Set myStringCell = .Rows(1).Find(myString)
colLetter = Split(myStringCell.Address, "$")(1)
Debug.Print myStringCell.Column 'if you can use the numeric column
Else
MsgBox "BOO!"
End If
End With