Consulting

Results 1 to 5 of 5

Thread: Formula Update

  1. #1
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location

    Formula Update

    Hello,
    I wrote a macro to avoid length formulas in cells. The macro does return correct values when I type in the Userdefined function in a cell but copies the previous value when I try to double click the right side bottom corner of the current cell to update the entire column.
    Can someone help me by pointing out what is wrong or what am I missing.
    [VBA]Function Class() As String
    'Marco used to determine the class of roadway segment V.V

    Dim i As Integer
    Dim Cl As Variant
    i = ActiveCell.Row
    Cl = Cells(i, 19).Value

    If (Cl = "F") Then
    Class = "F"
    ElseIf (Cl < 2) And Cl <> 0 Then Class = 1
    ElseIf (Cl >= 2 And Cl <= 4.5) Then Class = 2
    ElseIf Cl = 0 Then Class = 0
    Else: Class = 3
    End If

    End Function

    Function Facility_Type() As String
    ' MACRO used to determine the facility type of the roadwaysegment V.V

    Dim i As Integer
    i = ActiveCell.Row
    Dim Area As String
    Area = Cells(i, 16).Value
    Dim Fac_Type As String
    Dim Clas As String
    Clas = Class()

    If Clas = "F" Then
    Fac_Type = "FW"
    ElseIf (Cells(i, 16) = "UA" Or Cells(i, 16) = "TA") And Clas <> 0 Then
    Fac_Type = "S2WAC" & Cla
    ElseIf (Cells(i, 16) = "UA" Or Cells(i, 16) = "TA") And Clas = 0 Then
    Fac_Type = "UFH"
    ElseIf (Cells(i, 16) = "RUA" Or Cells(i, 16) = "RDA") And Clas <> 0 Then
    Fac_Type = "IFH"
    ElseIf (Cells(i, 16) = "RUA" Or Cells(i, 16) = "RDA") And Clas = 0 Then
    Fac_Type = "UFH"
    End If

    Facility_Type = Fac_Type

    End Function
    [/VBA]
    Thank you
    I am a Newbie, soon to be a Guru

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Function Class(rng As Range) As String
    'Marco used to determine the class of roadway segment V.V

    Dim i As Integer
    Dim Cl As Variant

    If (rng.Value = "F") Then
    Class = "F"
    ElseIf (rng.Value < 2) And rng.Value <> 0 Then Class = 1
    ElseIf (rng.Value >= 2 And rng.Value <= 4.5) Then Class = 2
    ElseIf rng.Value = 0 Then Class = 0
    Else: Class = 3
    End If

    End Function
    [/vba]

    =Class(S2)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Thank you XLD.

    I have another question, I am trying to write another macro that would look up values based on the arugment supplied. I want the argument value to vary between "S",'M" and "L", can you provide a link that talks about such information. Thank you
    I am a Newbie, soon to be a Guru

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just use an argument and a Case structure

    [vba]
    Function LookupValue(val as String)

    Select Case val

    Case "S": 'do something
    Case "M": 'do something else
    Case "L": 'do something other else
    End Select
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Thank you!!!
    I am a Newbie, soon to be a Guru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •