Reputation: 63
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
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
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
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