2014-03-26 3 views
0

범위를 범위로 설정하는 방법을 알아 내려고했지만 해당 범위의 값이 아닙니다. 이것은 중요한 경우 주당/1 인당 시간표 시간을 합산하는 것과 관련이 있습니다. 나는 문자 그대로 방금 지난 주에 vba 수업에서 나왔다. 그리고 나는 이것이 더 단순 할 것이라고 생각했다. 분명히 그런 식으로 보였습니다. 현재VBA에서 범위 값이 아닌 범위를 설정하는 방법

:

Option Explicit 
Dim RegularHours As Single 
Dim PWHours As Single 
Dim VacationHours As Single 
Dim EmployeeName As String 
Dim StartLocation As Range 

Public Sub BreakdownNumbers() 
Set StartLocation = Range("F2") 
    Do 
    AddRegularHours 
    Loop Until EmployeeName <> StartLocation.Offset(0, -5).Value 
Range("F18").Value = RegularHours 
End Sub 

Public Sub AddRegularHours() 
Dim StartDate As Date 
Dim EndDate As Date 
Dim CurrentDate As Date 

If StartLocation = Range("F2") Then              ' If this is the first entry calculated, Then... 

    StartDate = Range("B2").Value                ' Set the Default Start Date 
    EndDate = Range("C2").Value                ' Set the Default End Date 
    CurrentDate = Range("D2").Value               ' Set the Default Current Date 
    EmployeeName = Range("A2").Value              ' Set the Default Employee Name 

    StartLocation.Select 
    Do While CurrentDate >= StartDate And CurrentDate <= EndDate   ' Add up hours while the Current Date falls between the Start and End Dates 
     If EndDate = Selection.Offset(1, -3) Then          ' If the End Date in the Next Row is the Same as the Current Row, Then... 
      RegularHours = RegularHours + Selection.Value       ' Add Past Hours with Current Hours 
      Selection.Offset(1, 0).Select              ' Select the next row 
      StartDate = Selection.Offset(0, -4).Value         ' Set the new Start Date 
      EndDate = Selection.Offset(0, -3).Value          ' Set the new End Date 
      CurrentDate = Selection.Offset(0, -2).Value        ' Set the new Current Date 
     Else 
      RegularHours = RegularHours + Selection.Value       ' Add Past Hours with Current Hours 
      Selection.Offset(1, 0).Select              ' Select the next row down 
      StartLocation = ActiveCell.Address(0, 0)          ' Set the new Start Location 
      EmployeeName = Selection.Offset(0, -5).Value       ' Set new Employee Name 
      Exit Do                      ' Exit the Loop 
     End If 
    Loop 
Else                          ' If this is any other selection than the first entry 
     StartLocation.Select 
     StartDate = Selection.Offset(0, -4).Value          ' Set Start Date 
     EndDate = Selection.Offset(0, -3).Value           ' Set End Date 
     CurrentDate = Selection.Offset(0, -2).Value         ' Set Current Date 
     EmployeeName = Selection.Offset(0, -5).Value        ' Set Employee Name 

    Do While CurrentDate >= StartDate And CurrentDate <= EndDate  ' Add up hours while the Current Date falls between the Start and End Dates 
     If EndDate = Selection.Offset(1, -3) Then          ' If the End Date in the Next Row is the Same as the Current Row, Then... 
      RegularHours = RegularHours + Selection.Value       ' Add Past Hours with Current Hours 
      Selection.Offset(1, 0).Select              ' Select the next row 
      StartDate = Selection.Offset(0, -4).Value         ' Set the new Start Date 
      EndDate = Selection.Offset(0, -3).Value          ' Set the new End Date 
      CurrentDate = Selection.Offset(0, -2).Value        ' Set the new Current Date 
     Else 
      RegularHours = RegularHours + Selection.Value       ' Add Past Hours with Current Hours 
      Selection.Offset(1, 0).Select              ' Select the next row 
      StartLocation = ActiveCell.Address(0, 0)         ' Set the new Start Location 
      EmployeeName = Selection.Offset(0, -5).Value       ' Set new Employee Name 
      Exit Do                      ' Exit the Loop 
     End If 
    Loop 
End If 

최종 하위

이제 F2는 0의 값을 가진다. 나는 설정하면 StartLocation = 범위 (F2 ")는 두 가지 일을 수행합니다

1) 그것은 반환"개입 "때 그 위에 마우스를 가져 가면"0 "(F8)

2) I StartLocation을 주소 (0,0)로 업데이트하면 해당 범위 주소가 범위 ("F2")로 붙여 넣어집니다.

앞으로도 실제로 작동하지는 않지만 상태는 찾을 수 없습니다 왜 그것이 작동하지 않습니다. 그물에 찾은 모든 예제는 범위를 희미하게 다음 원하는 셀 또는 셀을 설정하십시오. 값의 접미사를. ;

코드 주석 부분에 죄송합니다. 나는 그것을 나의 시트에서 더 예쁘게 보일 것을 약속한다!

도움 주셔서 감사합니다.

답변

0

이 줄은 모호 :

If StartLocation = Range("F2") Then 

확인하려는 값이 동일하거나 StartLocation의 위치이 F2 있는지 확인하려고? 당신은 나는 항상 그런 다음 위치을 비교하는 것을 의미하는 경우 .Value

을 추가로 명시 적으로 그렇게하는 것을 선호 값의 비교 의미하는 경우

If StartLocation.Address() = Range("F2").Address() Then 

이 작동 것이다 (그들이에있어 가정 물론 같은 시트).

+0

안녕하세요. 답장을 보내 주셔서 감사합니다. 나는 그 위치를 확인하고 있는데, 그 값은 내가 갖고 싶어하는 것으로 보인다. .address()를 이전에 범위로 지정했으나 .value로 지정하지 않은 경우 명시해야하는 특별한 이유는 무엇입니까? –

+0

방금 ​​시도해 보았고 루프를 돌릴 때 텍스트 "F11"을 셀 F2에 붙여 넣고 숫자가 아닌 텍스트이기 때문에 유형 불일치 오류가 발생합니다. 쥐. =/ –

+0

메인 서브 블록에있는 두 개의 메인 If 블록의 목적은 무엇입니까 (If StartLocation = Range ("F2"Then Then ... ...)? 둘 다 어느 정도 동일하게 나타납니다. –

관련 문제