【发布时间】:2021-04-14 18:09:35
【问题描述】:
我希望在 Sheet1 Row1 中查找具有 2021 的单元格值,然后如果在 Sheet2("Monthly2") Row1 中找不到该单元格值,则将该日期粘贴到下一个可用单元格中。由于某种原因,此代码当前在 Sheet1 中找到最后一个带有 2021 的单元格,并且仅将其粘贴到 Sheets("Monthly2") Cell A1 中。
Sub Monthly2()
Dim Monthly2 As Worksheet
Dim celldate As Range, MatchingDate As Range
Dim wb As Workbook
Dim rng1 As Range, rng2 As Range
Dim rng As Range
Dim cell As Range
Set wb = ActiveWorkbook
Set rng1 = wb.Sheets("Sheet1").Range("A1:AD1")
Set rng2 = wb.Sheets("Monthly2").Range("A1:AD1")
For Each celldate In rng1
If celldate Like "*2021*" Then
For Each MatchingDate In rng2
If celldate = MatchingDate Then
ElseIf celldate <> MatchingDate Then
Sheets("Monthly2").Range("A1:AD1").Value = celldate
End If
Next MatchingDate
End If
Next celldate
End Sub
【问题讨论】:
-
你能具体说明你想要做什么吗?该代码原样不起作用,您无法比较这样的值。如果您想在某个范围内查找特定值,请查看使用 Application.Match 或 Find。
-
如果日期找不到怎么办?
-
如果找不到日期,我的其余代码将输入值和格式。那部分我已经处理好了。我正在尝试自动化一些代码,而不是一次编译每个日期。
-
@CorrieByrd 不要在 cmets 中发布代码,如果它与问题相关,请编辑问题并将其添加到那里。