第一次滿足條件時,將垂直範圍中的值依升序指派給另一個儲存格,然後退出

第一次滿足條件時,將垂直範圍中的值依升序指派給另一個儲存格,然後退出

我不確定我是否正確執行此操作,但我正在嘗試編寫一個子例程,該子例程查找符合我的條件的第一個值,然後將其複製到另一個單元格,然後停止查找更多值。

明確地說,我有一列按升序排列的 18 個月(從最舊到最新),並將其與今天的日期進行比較。

  • 2019年9月1日
  • 2019年10月1日
  • 2019年11月1日
  • 2019年12月1日
  • 2020年1月1日
  • 2020年2月1日
  • 2020年3月1日
  • 2020年4月1日
  • 2020年5月1日
  • 2020年6月1日
  • 2020年7月1日
  • 2020年8月1日
  • 2020 年 9 月 1 日
  • 2020年10月1日
  • 2020 年 11 月 1 日
  • 2020年12月1日
  • 2021 年 1 月 1 日
  • 2021 年 2 月 1 日

然後,我希望將今天日期之後的第一個月複製到另一個單元格,然後讓巨集停止搜尋滿足此條件的更多值。

這就是我的程式碼現在的樣子。

Sub Show_remaining_months()

        Dim TodaysDate As Long 'Today's Value
        
        Dim MonthCell As Range
        Dim i As Byte
        Dim EndHere As Byte
        
        
        Dim RestoreRefStyle As Integer
        Let RestoreRefStyle = Application.ReferenceStyle
        
        
        Application.ReferenceStyle = xlR1C1
        
        
        ThisWorkbook.Worksheets("subtotalizer(r-hrs)").Activate
        
        Let TodaysDate = Worksheets("subtotalizer(r-hrs)").Range("R1C5").Value ' TodaysDate = 44012
        
        
                    
                    
                    
                Let EndHere = 23
                                                     'Range(R6C3:R23C3)
                                For Each MonthCell In Range("R6C3:R" & (EndHere) & C3)
                                        
                                        For i = 6 To EndHere ' For i = 6 To 23
                                                             ' Which later then becomes i To EndHere.
                                                                                                                                                                             
                                                   If MonthCell.Value < TodaysDate Then
                                                   'Skip
                                                   i = i + 1
                                                   'i = 6 + 1 = 7
                                                   
                                                   Else
                                                   Let Range(R3C5).Value = MonthCell.Value
                                                   'i = i + 1
                                                   EndHere = i
                                                   
                                                   End If
                                                                                                 
                                       Next i
                                
                                Next MonthCell
 
    
        Application.ReferenceStyle = RestoreRefStyle


End Sub

我收到錯誤代碼 1004:應用程式定義或物件定義的錯誤

說實話,我覺得這個問題是我想太多了。我是 VBA 程式設計新手。

答案1

這次重寫完全符合我的要求。我想太多了。

Sub Show_remaining_months()


    Dim TodaysDate As Long 'Today's Value
    Dim StartDate As Range
            
    Dim MonthCell As Range
    Dim i As Byte
    Dim EndHere As Byte
        

        ThisWorkbook.Worksheets("subtotalizer(r-hrs)").Activate
        
        Let TodaysDate = Range("E1").Value
        Set CurrentStartDate = Range("E3")
        

              For Each MonthCell In Range("C6:C23")
                                        
                         If MonthCell.Value > TodaysDate Then
                            CurrentStartDate.Value = MonthCell.Value
                                    
                            Exit Sub
                                                 
                         End If
            
              Next MonthCell
 
End Sub

相關內容