GCC
GCC

Reputation: 295

Value in cell is changing after a message box

Here is my code

Sub errors()

Dim wsd As Worksheet

Set wsd = Worksheets("Sheet1")

    With Forc

        If Len([F4]) <> 4 Then
            [F4] = MsgBox("add values")

        End If

    End With


End Sub

My code reads that if in cell F4, if the number of digits is not 4 to display an error message. The error message displays properly but when I press "ok" on the message box, the value in F4 changes to 1. I do not want it to change to 1. Why is it doing this and how do I fix it?

Upvotes: 0

Views: 62

Answers (3)

Matteo NNZ
Matteo NNZ

Reputation: 12645

It is doing this because you're saying:

[F4] = MsgBox("add values")

... meaning setting equal the range F4 the value of the MsgBox. The MsgBox returning vbOk (enumerate value corresponding to 1), the value is set to 1.

You just need to write:

MsgBox("add values")

Upvotes: 1

Yuki
Yuki

Reputation: 212

Please try following code below:

Sub errors()
Dim wsd As Worksheet
Set wsd = Worksheets("Sheet1")
    With Forc
        If Len([F4]) <> 4 Then
            MsgBox ("add values")
        End If
    End With
End Sub

Any questions or concerns, feel free to let me know. Looking forward to hearing from you.

Upvotes: 0

Dinu Kuruppu
Dinu Kuruppu

Reputation: 165

Dont equel the range [F4] to your message Box, that's why its taking the return value

Sub errors()

Dim wsd As Worksheet

Set wsd = Worksheets("Sheet1")

    With Forc

        If Len([F4]) <> 4 Then
            MsgBox("add values")

        End If

    End With


End Sub

Upvotes: 0

Related Questions