naturlich
naturlich

Reputation: 63

Excel VBA copy and paste a specific cell to another Sheet

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

Upvotes: 1

Views: 1283

Answers (3)

REA
REA

Reputation: 189

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

Upvotes: 0

user4039065
user4039065

Reputation:

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

Upvotes: 1

JohnyL
JohnyL

Reputation: 7122

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

Upvotes: 2

Related Questions