2016-10-11 4 views
0

원하는 매크로는 다음과 같습니다. 이름 | 성 | 이 문자열을 반환하는 경우에도 userNameBuilder 함수를 호출하고 여기에 코드가 불평 내 userNameBuilder 기능은 문자열을형식 불일치 Excel/Outlook vba

Dim firstName, lastName As String 
Set sh = ActiveSheet 
Dim counter = 0 
For Each entry In exUser.GetDirectReports() 'exUser is an exchangeUser 
    counter = counter + 1 
    firstName = entry.GetExchangeUser.firstName 
    lastName = entry.getExchangeUser.lastName 
    sh.Cells(counter, 1).value = firstName 
    sh.Cells(counter, 2).value = lastName 
    sh.Cells(counter, 3).value = userNameBuilder(firstName, lastName) 
Next 

를 기대 때 오류가 점점 오전

username을.

"sh.Cells (counter, 3) .value = userNameBuilder (firstName, lastName)"을 주석 처리하면 코드가 올바르게 실행되고 사용자 이름이 만들어지지 않습니다.

사용자 이름은 성의 처음 5 글자와 이름의 첫 글자를 더한 것입니다. 성이 너무 짧으면 사용자 이름이 6 자일 때까지 처음부터 시작하는 이름의 문자로 채워집니다.

Public Function userNameBuilder(ByVal firstName As String, ByVal lastName As String) As String 
    Dim newLastName As String 
    Dim newUserName As String 
    If (lastName >= 5) Then 
     newLastName = Left(lastName, Len(lastName) - 5) 
     userNameBuilder = (newLastName & Left(firstName, 1)) 
    ElseIf (lastName < 5 && lastName > 0) Then 
     userNameBuilder = lastName & Left(firstName, 5 - (Len(lastName))) 
    Else 
     userNameBuilder = vbNullString 
    End If 
End Function 
+0

이이 문제와 관련되지 않을 수 있습니다 좋아하지만 _ "처음 5 개 문자를 사용해야합니다 마지막 이름 "_이 (가) newLastName = 왼쪽 (성, 5)이됩니다. – user3598756

+0

아 맞다. –

답변

0

대신

If (lastName >= 5) Then 

당신은

If (Len(lastName) >= 5) Then 

Public Function userNameBuilder(ByVal firstName As String, ByVal lastName As String) As String 
    Dim newLastName As String 
    Dim newUserName As String 
    If (Len(lastName) >= 5) Then 
     newLastName = Left(lastName, 5) 
     userNameBuilder = (newLastName & Left(firstName, 1)) 
    ElseIf (Len(lastName) < 5 && Len(lastName) > 0) Then 
     userNameBuilder = lastName & Left(firstName, 5 - (Len(lastName))) 
    Else 
     userNameBuilder = vbNullString 
    End If 
End Function