JBinson88
JBinson88

Reputation: 123

Combining two vba functions

I have the following VBA functions in Excel (datecleanup and date1) that I would like to combine together into date1. Or preferably take the logic in dateclean and put into date1. This I'm sure is very simple, however I am new to VBA and not sure how I can accomplish this.

datecleanup function:

Function datecleanup(inputdate As Variant) As Variant

If Len(inputdate) = 0 Then
  inputdate = "01/01/1901"
Else
  If Len(inputdate) = 4 Then
    inputdate = "01/01/" & inputdate
  Else
    If InStr(1, inputdate, ".") Then
        inputdate = Replace(inputdate, ".", "/")
    End If
    dateclean = Split(strInput, Chr(32))(0)
  End If
End If
End Function

date1 function:

datecleanup = inputdate

Function date1(strInput) As String
 date1 = Split(strInput, Chr(32))(0)

End Function

I would like the date1 logic to occur as the final part of the dateclean function. How can I accomplish this? Thanks very much!

EDIT:

This is the correct datecleanup function:

Function datecleanup(inputdate As Variant) As Variant

If Len(inputdate) = 0 Then
    inputdate = "01/01/1901"
Else
  If Len(inputdate) = 4 Then
    inputdate = "01/01/" & inputdate
  Else
    If InStr(1, inputdate, ".") Then
        inputdate = Replace(inputdate, ".", "/")
    End If

  End If
End If

datecleanup = inputdate

End Function

Upvotes: 0

Views: 1160

Answers (1)

JNevill
JNevill

Reputation: 50034

Here's some cleaned up logic with it all combined into a single function:

Function datecleanup(inputdate As Variant) As String

    If Len(inputdate) = 0 Then
        inputdate = "01/01/1901"
    ElseIf Len(inputdate) = 4 Then
        inputdate = "01/01/" & inputdate
    ElseIf InStr(1, inputdate, ".") Then
        inputdate = Replace(inputdate, ".", "/")
    End If  

    datecleanup = Split(inputDate, Chr(32))(0)
End Function

Alternatively, you could keep them separate functions and just call the datecleanup function from within your date1 function:

Private Function datecleanup(inputdate As Variant) As String
    If Len(inputdate) = 0 Then
        inputdate = "01/01/1901"
    ElseIf Len(inputdate) = 4 Then
        inputdate = "01/01/" & inputdate
    ElseIf InStr(1, inputdate, ".") Then
        inputdate = Replace(inputdate, ".", "/")
    End If  

    datecleanup = inputdate     
End Function

Function date1(strInput) As String
    date1 = Split(datecleanup(strInput), Chr(32))(0)
End Function

This is nice because it keeps the logic separate (if that's desirable here...)

Upvotes: 2

Related Questions