C Bourke
C Bourke

Reputation: 23

Compile error: Sub or function not defined (MRound)

I've built the following function to change the format of a bearing from DD.MMSS to DD°MM'SS", however I am getting the above mentioned error when the function reaches the MRound command embedded within the custom function I am testing.

I have read a number of questions relating to this type of compile error on stack overflow and suspect the error is related to VBA not correctly referencing the MRound function however I do not know how to resolve it, if this is the case.

Your help is greatly appreciated!

Function Test(Bearing_DMS As Single) As String

Dim degrees As String
Dim minutes_working As Single
Dim minutes As String
Dim seconds_working As Single
Dim seconds As String

    'Determine degrees value
    If Int(Bearing_DMS) < 10 Then
        degrees = "    " & Int(Bearing_DMS)
        ElseIf Int(Bearing_DMS) >= 10 And Int(Bearing_DMS) < 100 Then
        degrees = "  " & Int(Bearing_DMS)
        Else
        degrees = Int(Bearing_DMS)
    End If

    'Determine minutes_working value
    minutes_working = (Bearing_DMS - degrees) * 100

    'Determine minutes value
    If minutes_working < 1 Then
        minutes = "00"
        ElseIf minutes_working >= 1 And minutes_working < 10 Then
        minutes = "0" & Int(minutes_working)
        Else
        minutes = Int(minutes_working)
    End If

    'Determine seconds_working value
    seconds_working = (minutes_working - minutes) * 100

    'Determine seconds value
    If seconds_working < 1 Then
        seconds = "00"
        ElseIf seconds_working >= 1 And seconds_working < 10 Then
        seconds = "0" & MRound(seconds_working, 1)
        Else
        seconds = MRound(seconds_working, 1)
    End If

    'Determine final value to display

    Test = degrees & "° " & minutes & "' " & seconds + Chr(34)

End Function

Upvotes: 2

Views: 303

Answers (2)

Joubarc
Joubarc

Reputation: 1216

MROUND is a cell formula, not a VBA function. You can access it from VBA using WorksheetFunction.MRound

Upvotes: 2

Siddharth Rout
Siddharth Rout

Reputation: 149315

MRound is not a VBA function. Change MRound(seconds_working, 1) to WorksheetFunction.MRound(seconds_working, 1)

Upvotes: 2

Related Questions