기준이 처음 충족되면 수직 범위의 값을 오름차순으로 다른 셀에 할당한 다음 종료합니다.

기준이 처음 충족되면 수직 범위의 값을 오름차순으로 다른 셀에 할당한 다음 종료합니다.

이 작업을 제대로 수행하고 있는지 확신할 수 없지만 기준에 맞는 첫 번째 값을 찾은 다음 이를 다른 셀에 복사한 다음 더 이상 값 찾기를 중지하는 서브루틴을 작성하려고 합니다.

명확하게 말하면 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

관련 정보