1

I want to see if a specific cell is the value I expect, if so, I want to paste it to another sheet. But my code went wrong somehow. Here is my code:

Private Sub CommandButton1_Click()

    Sheets("Sheet3").Select
    If Cells(2,6).Value == 25 Then
        Cells(2, 6).Select
        Selection.Copy

        Sheets("Sheet1").Select
        Cells(4, 1).Paste
    End If

End Sub

3 Answers 3

2
Private Sub CommandButton1_Click()
    With Sheets("Sheet3")
        If .Cells(2, 6).Value = 25 Then
            .Cells(2, 6).Copy Sheets("Sheet1").Cells(4, 1)
        End If
    End With
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

VBA uses a single = sign for comparison.

Private Sub CommandButton1_Click()
    If workSheets("Sheet3").Cells(2,6).Value = 25 Then
        workSheets("Sheet1").Cells(4, 1) = 25
    End If
End Sub

Comments

0

You can write an interactive window to input a value that may change.

Private Sub CommandButton1_Click()

    Dim exp_val As Integer
    exp_val = InputBox("What's the value you expect?")
    If Worksheets("Sheet3").Cells(2, 6).Value = exp_val Then
        Worksheets("Sheet2").Cells(4, 1).Value = exp_val
    End If

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.