简体   繁体   中英

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
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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM