How to convert string to double ? RRS feed

  • Question

  • Hi,

    I am populating values by using GetField method at macro, field is of Duration type so it returns result in "20 days" format. For further calculation i need to convert these values to double i.e 20.

    Please suggest me how can i convert these values to double.



    C Mahone

    Friday, November 8, 2013 1:55 PM

All replies

  • C.Mahone,

    Using the CDbl function does not work directly so a couple of interim steps are needed. The following code should do it.

    Sub ConGetToDbl()
    Dim t As Task
    Dim val As String
    Dim p1 As Integer
    For Each t In ActiveProject.Tasks
        If Not t Is Nothing Then
            val = t.GetField(pjTaskDuration)
            p1 = InStr(1, val, " ")
            t.Number1 = CDbl(Mid(val, 1, p1))
        End If
    Next t
    End Sub

    Personally, if I were trying to get the Duration field value I would not use the GetField function, rather I'd work directly with the duration property of the task object, noting that its value will be in minutes.


    Friday, November 8, 2013 3:30 PM
  • I agree with John, T.Duration/60/activeproject.HoursPerDay gives you the Duration directly.

    Otherwise a quicker way to convert to a number is:

    val=Val(strDur)    'Converts to number

    cDbl(val) forces conversion to double

    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    Friday, November 8, 2013 11:09 PM