我正在尝试执行以下操作,但没有成功。
我有两个标签,计数和输入。我需要做的是从选项卡一的文件代码中插入一个基本上是vlookup的公式,以查看每天生成的代码(在选项卡2中)在正确的日期与它们匹配。
到目前为止,我的代码是这样做的:要求在选项卡1中输入需要“更新” =匹配的日期。这部分有效,宏会找到正确的日期。现在,我正在努力的是告诉宏:从找到的日期开始,偏移一行,然后在查找列中插入所有非空行的公式。
据此是我的代码。我认为在这种范围的定义中我做错了,因为是可变的。感谢您的所有帮助!
enter code here
Sub Macro2()
Macro2 Macro
Dim myValue As Date
myValue = InputBox("Please enter the date you want to update")
Sheets("Count").Range("A1").Value = myValue
Dim ra As Range
Dim date1 As Date
date1 = Range("A1")
Set ra = Cells.Find(What:=date1 _
, LookIn:=xlFormulas _
, LookAt:=xlPart _
, SearchOrder:=xlByRows _
, SearchDirection:=xlNext _
, MatchCase:=False _
, SearchFormat:=False)
If ra Is Nothing Then
MsgBox ("Not found")
Else
'MsgBox (ra.Address)
Dim rws As Range
rws = Cells(Rows.Count, "B").End(xlUp).Row
Range(ra.Offset(1, 0) & rws).Formula = "=IFERROR(VLOOKUP($B3,'Input'!$B:$B,1,FALSE),"""")"
End If
End Sub
您的第一个问题是将rws设置为范围,但随后尝试为其分配行号。范围可容纳一个单元格或多个单元格,但行号是数字值。因此它将失败。而是将其昏暗为Long
,以便它可以容纳数字值。
第二,您尝试将找到的单元格偏移一个,然后向其添加最后一行。如果这是尝试使用整个范围,则不是这样。默认情况下,Offset
属性将返回单元格的值,而不是单元格本身,除非使用.Address
或任何类似形式指定。同样,您不能一次将奇异值或公式分配给一个范围。这将必须使用循环来完成。
下面的代码是我所需要的近似值,如果您在评论中回答我的问题,我可以对此进行细化:
Sub Macro2()
Dim myValue As Date
myValue = InputBox("Please enter the date you want to update")
Sheets("Count").Range("A1").Value = myValue
Dim ra As Range
Dim date1 As Date
date1 = Range("A1")
Set ra = Cells.Find(What:=date1 _
, LookIn:=xlFormulas _
, LookAt:=xlPart _
, SearchOrder:=xlByRows _
, SearchDirection:=xlNext _
, MatchCase:=False _
, SearchFormat:=False)
If ra Is Nothing Then
MsgBox ("Not found")
Else
'MsgBox (ra.Address)
Dim rws As Long
Dim cel As Range
rws = Cells(Rows.Count, ra.Column).End(xlUp).Row
For Each cel In Range(Cells(ra.Offset(1, 0).Row, ra.Column), Cells(rws, ra.Column))
If cel <> "" Then cel.Formula = "=IFERROR(VLOOKUP($B" & (cel.Row - 2) & ",'Input'!$B:$B,1,FALSE),"""")"
Next cel
End If
End Sub