在行内查找Datevariables并返回Column VBA

我正在寻找一行内的variables,并返回列引用。 我到目前为止写的代码是;

Dim VarianceDate As String VarianceDate = Sheets("Summary").Range("C12").Value Rows("6").Find(What:=VarianceDate, After:=ActiveCell, LookIn:=xlFormulas _ , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate TargetCol = ActiveCell.Column 

在这个例子中,Variancedate是01/06/2015,但是当我在VBA中join代码时,它什么都不返回。 除了当我手动search它find正确的单元格。

最终我想使用TargetCol参考来帮助我将正确的数据提取到另一个工作簿中。

任何帮助将是非常appreicated。

谢谢

您需要提及的是,search词是一个date,如果活动单元不在您的查找范围内,则会由于包含After:= ActiveCell而引发错误

尝试下面的代码

 Sub FindDateCol() Dim VarianceDate As String: VarianceDate = Sheets("Summary").Range("C12").Value Dim TargetCell As Range, TargetCol As Integer Set TargetCell = Rows("6").Find(What:=CDate(VarianceDate), LookIn:=xlFormulas, LookAt:=xlPart) If Not TargetCell Is Nothing Then TargetCol = TargetCell.Column MsgBox TargetCol End Sub