0

I'm trying to get the right most letter from a variable I have set and then assign either Male or Female to the result. Below is a snippet of the code I've tried.

Set Gender = rFirst.Offset(1, 5)

Worksheets("E'ee Details").Range("E1").Value = "=if(right(" & Gender & ",1)=""M"",""Male"",if(right(" & Gender & ",1)=""F"",""Female"",""""))"

This is resulting in #NAME? being displayed.

12
  • 1
    You are putting the value of Gender into your formula, as a cursory examination of the formula will tell you, which the sheet will not understand. You need to use the address property to return the cell reference. Commented Jan 3, 2018 at 12:28
  • From your use of set, i'm assuming you're doing this in VBA, why not just use an IF ELSE IF statement to set the value? Commented Jan 3, 2018 at 12:31
  • in the formula, change Gender to Gender.address and it should work fine. Commented Jan 3, 2018 at 12:32
  • Thanks SJR, I have done that by adding .Address after Gender but now it's returning a blank cell Commented Jan 3, 2018 at 12:40
  • That means the last character is neither "M" nor "F". Commented Jan 3, 2018 at 12:42

2 Answers 2

1

Amend as below

Worksheets("E'ee Details").Range("E1").Value = "=if(right(EmployeeAll!" & gender.Address & ",1)=""M"",""Male"",if(right(EmployeeAll!" & gender.Address & ",1)=""F"",""Female"",""""))"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks SJR, this worked perfectly. Thanks everyone else for input
1

Your approach involves creating a worksheet equation and then using Evaluate() to calculate it. Here is an alternative approach:

Sub jufg()
    Dim Gender As Range, sexCode As String
    Dim rFirst As Range
    Set rFirst = Range("A1")

    Set Gender = rFirst.Offset(1, 5)
    sexCode = Right(Gender.Value, 1)

    With Worksheets("E'ee Details").Range("E1")
        If sexCode = "M" Then
            .Value = "Male"
        ElseIf sexCode = "F" Then
            .Value = "Female"
        Else
            .Value = ""
        End If
    End With
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.