2013-11-01 1 views
0

안녕하세요 모두가 친구들 사이의 주 수,엑셀 VB 내가 VB 매우 새로 온 두 개의 셀

을 계산하고 나는 매우 간단 뭔가를 만들려고 웹의 코드의 비트를 복용했습니다. 나는 날짜 값을 가진 두 셀 사이의 주 수를 계산하는 데 탁월한 노력을하고 있습니다. 첫 번째 셀은 고정 된 위치에 있고 두 번째 셀은 해당 열 내의 마지막 셀 범위입니다.

Last은 웹에서 끼어 드는 기능입니다. 이 일을 전혀 할 수 없기 때문에 어떤 도움을 주시면 감사하겠습니다.

Sub test_date_calc() 



    Dim LastCell As String 
    Dim nwks As Integer 
    Dim rng As Range 

    Set rng = Sheets("data13").UsedRange 
    LastCell = Last(3, rng) 
    nwks = (Cells(3, 2) - LastCell.Value)/7 
    If nwks > 13 Then 

    MsgBox "greater" 
    Else 

    MsgBox "Less" 

    End If 
    End Sub 

나는 당신의 오류가이 라인으로 인해 생각

Function Last(choice As Long, rng As Range) 
'Ron de Bruin, 5 May 2008 
' 1 = last row 
' 2 = last column 
' 3 = last cell 
    Dim lrw As Long 
    Dim lcol As Long 

    Select Case choice 

    Case 1: 
     On Error Resume Next 
     Last = rng.Find(What:="*", _ 
         After:=rng.Cells(1), _ 
         Lookat:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByRows, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Row 
     On Error GoTo 0 

    Case 2: 
     On Error Resume Next 
     Last = rng.Find(What:="*", _ 
         After:=rng.Cells(1), _ 
         Lookat:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByColumns, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Column 
     On Error GoTo 0 

    Case 3: 
     On Error Resume Next 
     lrw = rng.Find(What:="*", _ 
         After:=rng.Cells(1), _ 
         Lookat:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByRows, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Row 
     On Error GoTo 0 

     On Error Resume Next 
     lcol = rng.Find(What:="*", _ 
         After:=rng.Cells(1), _ 
         Lookat:=xlPart, _ 
         LookIn:=xlFormulas, _ 
         SearchOrder:=xlByColumns, _ 
         SearchDirection:=xlPrevious, _ 
         MatchCase:=False).Column 
     On Error GoTo 0 

     On Error Resume Next 
     Last = rng.Parent.Cells(lrw, lcol).Address(False, False) 
     If Err.Number > 0 Then 
      Last = rng.Cells(1).Address(False, False) 
      Err.Clear 
     End If 
     On Error GoTo 0 

    End Select 
End Function 
+0

에 대한 문서를 볼 수 있습니다 당신이 말할 때, 작동하지 않는 .. 당신이 오류를 얻을, 또는 잘못된 결과를 않습니다합니까? 더 많은 정보를 제공하지 않으면 도움이 어렵습니다. – Sam

+0

LastCell이 강조 표시된 "잘못된 한정자"가 나타납니다. "Last"함수에 대한 코드도 도움이 될 수 있습니다 –

+0

코드가 여기에 적합하지 않습니다 - 죄송합니다. –

답변

3

도움이 될 수 아래 Last 기능 : 그것은 필요하지 않도록

nwks = (Cells(3, 2) - LastCell.Value)/7 

LastCell 문자열입니다 .Value

nwks = (Cells(3, 2) - LastCell)/7 

VBA에서 DATEDIFF 함수를 사용할 수 있습니다.

DateDiff(interval, date1, date2, [firstdayofweek], [firstweekofyear]) 

사용 예 :

nwks = DateDiff("ww", Cells(3, 2).Value, LastCell.Value) 

당신은 DATEDIFFHERE

+0

고마워요.하지만 정상에있는 코드가 왜 작동하지 않는지 정말보고 싶었습니다. –

+0

@ VBA_noob240 문제의 원인을 찾은 것 같습니다. 내 대답을 편집했습니다. – Sam

+0

"nwks = (Cells (3, 2) .Value - Lastcell)/7"에 "type mismatch"라는 새로운 오류 메시지가 표시되는만큼 작동합니다. 그동안 도움을 주셔서 감사합니다. –