Excel VBA查找方法出错了?
我的表
我在一段时间内发展了一些依赖于同一个macros的工作表。 它会find标题,并从选定的列复制列。
该工作表用于比较两组不同的数据,而VBA中的查找function则经常使用。
我的问题
问题在于Find
function,而它引用同一列,有两个不同的名称,有一个轻微的比较。
它发生在两列,在一定程度上有一个名称比较,如下所示:
Sub CopyPasteDataLookingForHeader() Dim sht As Worksheet Set sht = Sheets("Put in data") 'I'm trying to find the column header Total Lineamount within my sheet FindTotalLineVAT = sht.Range("1:1").Find("Total Lineamount").Address(False, False, xlA1) TotalLineVAT = Application.WorksheetFunction.Substitute(FindTotalLineVAT, 1, "") 'FindTotalLineVAT will return Cell "J1" 'TotalLineVAT will return column "J" 'I'm trying to find the column header Total Lineamount excl VAT within my sheet FindTotalLineXVAT = sht.Range("1:1").Find("Total Lineamount excl VAT").Address(False, False, xlA1) TotalLineXVAT = Application.WorksheetFunction.Substitute(FindTotalLineXVAT, 1, "") 'FindTotalLineXVAT will return Cell "J1" 'TotalLineXVAT will return column "J" End Sub
我的目标
我想要这个公式返回单元格和列的两个不同的结果。 “查找”function应该返回两组不同的单元格,因为名称在某种程度上是不同的。
有人可以解释为什么“查找”不search整个名称,但只有一点点?
请让我知道,你的回答非常感谢:)
您得到两倍相同的结果,默认情况下Find
函数中的LookAt
参数设置为xlPart
。 尝试在Find
函数中将LookAt
参数设置为xlWhole
,如下所示:
FindTotalLineVAT = sht.Range("1:1").Find("Total Lineamount", Lookat:=xlWhole).Address(False, False, xlA1) FindTotalLineXVAT = sht.Range("1:1").Find("Total Lineamount excl VAT", Lookat:=xlWhole).Address(False, False, xlA1)
它现在应该按预期工作。