How to Create an Event Change that will Let me copy and paste cell ranges based on the text entered into another cell

ds9703c

New Member
Joined
Jun 19, 2018
Messages
2
Hi,

I need some help. I'm trying to write a VBA code that will allow me to copy and paste a range of cells from sheet to another based on the text entered into a cell.

So far I have this but it is not working

Code:
[COLOR=#212121][FONT=wf_segoe-ui_normal]Private Sub Worksheet_Change(ByVal Target As Range)[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]Set Target = Target.Cells(31, 3)[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]If Not Intersect(Target, Range("C31")) Is Nothing Then[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]If Taregt.Value = "Farm Manure" Then[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]Sub CopyPasteToAnotherSheet()[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]Sheets("DefaultData").Range("D7:D12").Copy[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]Sheets("AD").Activate[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]Range("L41:L46").Select[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]ActiveSheet.Paste[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]End Sub[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]End If[/FONT][/COLOR]
[COLOR=#212121][FONT=wf_segoe-ui_normal]End If[/FONT][/COLOR]

I want the cells D7:D12 in the Sheet DefaultData to be copied to cells L41:L46 on the AD sheet
when Farm Manure is entered to cell C31 in the AD sheet

Thanks!
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi & welcome to the baord
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("C31")) Is Nothing Then
   If Target.Value = "Farm Manure" Then
      Sheets("DefaultData").Range("D7:D12").Copy Range("L41")
   End If
End If
End Sub
 
Upvote 0
Hi! thanks for your fast reply

when I try this code it says run-time error '9': Subscript out of range and highlights this line
Sheets("DefaultData").Range("D7:D12").Copy Range("L41")
 
Upvote 0
Do you have a sheet called "DefaultData"?
The spelling must be exactly the same as your sheet name
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,175
Members
449,071
Latest member
cdnMech

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