【问题标题】:Loop Through Dates on First Row and match if on first row of other sheet循环第一行的日期并匹配其他工作表的第一行
【发布时间】: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 中发布代码,如果它与问题相关,请编辑问题并将其添加到那里。

标签: excel vba


【解决方案1】:

未经测试

Dim wb as Workbook
Dim rng1 as Range, rng2 as Range
Dim rng as Range, celldate as Range

Set wb = ActiveWorkbook
Set rng1 = wb.Sheets("Sheet1").Range("A1:AD1")
Set rng2 = wb.Sheets("Monthly").Range("A1:AD1")

For Each celldate In rng1
    If celldate Like "*2021*" Then
        Set rng = rng2.Find(celldate)
        If rng is Nothing Then
           ' not found
        Else
           ' found do something
        Endif
    End If
Next

【讨论】:

    【解决方案2】:

    为什么不尝试比较 2021 年的值与最小双精度值

    例如。日期:2021 年 1 月 1 日是 44197.00 作为双精度值

    如果您比较您的单元格>= 44197,那么至少是 2021 年

    您也可以将年末 44561.00 添加为双精度值

    Const YearBegin = 44197, YearEnds = 44561
    
    Sub IterareDates()
        For Each CellDate In ActiveWorkbook.Sheets("Sheet1").Range("A1:AD1")
            'this if, indicate that value appertain to 2021
            If CellDate.Value >= YearBegin And CellDate.Value <= YearEnds Then
                'I dont understand those lines, I suppose you are trying to compare cell
                'by cell in 2 sheets, if im right, you are comparing 2 entire ranges,
                'you should compare each cell separately or sum values and if diferent set         which you need
    
                If ThisWorkbook.Application.WorksheetFunction.CountIf(Range("A1:AD1"),  CellDate.Value) > 0 Then
                    'exist
                Else
                    'dont
                End If
    
    
            End If
        Next CellDate
    End Sub
    

    【讨论】:

    • 我正在尝试比较两张不同工作表中的单元格值。如果一个日期位于 Sheet2 中的 Sheet1 中,则循环到下一个日期。
    • 好的,如果您需要知道范围是否包含该日期,只需添加一个计数如果像在答案中一样,我已编辑
    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    相关资源
    最近更新 更多