Excel VBA将特定单元格复制并粘贴到另一个工作表

时间:2018-05-06 12:31:20

标签: excel vba

我想查看特定单元格是否是我期望的值,如果是,我想将其粘贴到另一张表格中。但我的代码以某种方式出错了。 这是我的代码:

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

VBA使用单个=符号进行比较。

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

答案 2 :(得分:0)

您可以编写一个交互式窗口来输入可能会更改的值。

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
相关问题