VBA mismatch error 13 after adding paste condition

MarcianoPL

New Member
Joined
Sep 23, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Good afternoon everyone! :)

I was hoping that you could help me solving error with my code.
What I want to achieve is when user type "ok" in any cell the code will copy range directly above target cell and paste it in target cell. And this part is working just fine, it copy-paste as desired, however after paste immediately run-time error 13 pops out. What's even more wierd for me is that after clicking "end" i will get this error for second time if i do anything with pasted range. Debug function highlights IF statement.
Can't make heads or tails out of it...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target = "ok" Then
StartCell = Target.Offset(-7, 0).Address
EndCell = Target.Offset(-1, 0).Address
Range(StartCell, EndCell).Copy Target

Else

End If

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi & welcome to MrExcel.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target = "ok" And Target.Row > 7 Then
      Target.Offset(-7).Resize(7).Copy Target
   End If
End Sub
 
Upvote 0
Solution
Hi & welcome to MrExcel.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target = "ok" And Target.Row > 7 Then
      Target.Offset(-7).Resize(7).Copy Target
   End If
End Sub
Works like a charm! Thank you sooo much!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,886
Messages
6,122,093
Members
449,064
Latest member
Danger_SF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top