2013-05-09 3 views
0

내 코드는 사용자가 입력 한 날짜의 제품 변환 횟수를 찾을 수 있도록 Excel에서 입력 한 시작일과 종료일을 찾기위한 코드입니다. 사용자가 행에 존재하는 시작 및 종료 날짜를 입력하면 올바르게 작동합니다. 문제는 사용자가 행에 존재하지 않는 시작 및 종료 날짜를 입력 할 때 제품 변환 수 = 0을 제공하는 것입니다. 예를 들어, 내 데이터는 1/20/2013에서 1/28/2013까지의 날짜와 10 개의 제품 변환으로 구성됩니다. 사용자가 시작 날짜 = 2013 년 1 월 1 일, 종료 날짜 = 1 월 29 일 (행에없는 날짜)를 입력하면 전환 수는 0입니다. 날짜가 행에 있으면 프로그램이 자동으로 가장 가까운 날짜로 점프합니다. 여기 내 코드입니다 :입력 한 날짜가없는 경우 가장 가까운 날짜로 점프

Dim rowFound As Variant 
Dim startDate As String, endDate As String, startDateRow As Long 
Dim endDateRow As Long, product As String, convNo As Long 

Set ws2 = ActiveWorkbook.Sheets("Products Conversion") 
Set wsMain = ActiveWorkbook.Sheets("Main Menu") 

ws2.Activate 
lastrow2 = ws2.Range(Range("A1"), Range("A65535").End(xlUp)).count ' find lastrow 
wsMain.Activate 

startDate = Me.txtStartDate.Value 

endDate = Me.txtEndDate.Value 

On Error Resume Next 
If txtStartDate <> "" Or txtEndDate <> "" Then 

For i = 3 To lastrow2 

    If CDate(startDate) = ws2.Cells(i, 1).Value Then 
     startDateRow = i ' row where start date is 
     Exit For 
    End If 

Next 


For j = lastrow2 To 3 Step -1 

    If CDate(endDate) = ws2.Cells(j, 1).Value Then 
     endDateRow = j  ' row where end date is 
     Exit For 
    End If 

Next 

For k = startDateRow To endDateRow - 1 

    product = ws2.Cells(k, 6).Value 

    If product <> ws2.Cells(k + 1, 6).Value Then 
     convNo = convNo + 1 'number of conversion 
    End If 

Next 

Else 
MsgBox "Please enter both date!", vbOKOnly + vbCritical 
End If 

Me.txtConvNo.Value = convNo 

답변

1

아니라 마침내 내가 그거야 몇 가지 루프를 추가 알아낼 수

Dim rowFound As Variant 
Dim startDate As String, endDate As String, startDateRow As Long 
Dim endDateRow As Long, product As String, convNo As Long 

Set ws2 = ActiveWorkbook.Sheets("Products Conversion") 
Set wsMain = ActiveWorkbook.Sheets("Main Menu") 

ws2.Activate 
lastrow2 = ws2.Range(Range("A1"), Range("A65535").End(xlUp)).count ' find lastrow 
wsMain.Activate 

startDate = Me.txtStartDate.Value 

endDate = Me.txtEndDate.Value 

On Error Resume Next 
If txtStartDate <> "" Or txtEndDate <> "" Then 

resume1: 
For i = 3 To lastrow2 

    If CDate(startDate) = ws2.Cells(i, 1).Value Then 
     startDateRow = i ' row where start date is 
     Exit For 
    End If 

Next 

If startDateRow = 0 Then 'date entered not found in the row 
    startDate = CDate(startDate) + 1 
    GoTo resume1 
End If 

resume2: 
For j = lastrow2 To 3 Step -1 

    If CDate(endDate) = ws2.Cells(j, 1).Value Then 
     endDateRow = j ' row where end date is 
     Exit For 
    End If 


Next 

If endDateRow = 0 Then 'date entered not found in the row 
    endDate = CDate(endDate) - 1 
    GoTo resume2 
End If 

Next 

For k = startDateRow To endDateRow - 1 

product = ws2.Cells(k, 6).Value 

If product <> ws2.Cells(k + 1, 6).Value Then 
    convNo = convNo + 1 'number of conversion 
End If 

Next 

Else 
MsgBox "Please enter both date!", vbOKOnly + vbCritical 
End If 

Me.txtConvNo.Value = convNo 
vba excel-vba 
관련 문제