VBA to copy data from multiple cells between sheets

Hazman_08

New Member
Joined
Aug 30, 2023
Messages
12
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have a workbook with 2 sheets. Sheet1 has range D19:I22. Sheet2 has range E5:J8. What I want to do is have data entered into one cell copied to a specific other cell on the other sheet, and vice versa. For example, Sheet1 D19 copied to Sheet2 E5, or Sheet2 E5 copied to Sheet1 D19. I think this would be a Worksheet_Change of some sort. Can someone please tell me if/how this can be done?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You would need 2 Worksheet Change event modules, one for sheet1, one for sheet 2. I'll assume you know how/where to add sheet module codes, and please note that I've used the sheet code names (e.g. Sheet1) not the sheet names (tab names).

This one in the sheet 1 module:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("D19:I22"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Target.Copy Sheet2.Range(Target.Address).Offset(-14, 1) '<-- *** Sheet code name used ***
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub


This one in the sheet 2 module:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("E5:J8"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Target.Copy Sheet1.Range(Target.Address).Offset(14, -1) '<-- *** Sheet code name used ***
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0
Solution
Thanks Kevin. The sheet names have spaces in them. How do I handle that when replacing "Sheet1" and "Sheet2"? When I type without spaces, I get this error
1693446568132.png
 
Upvote 0
Thanks Kevin. The sheet names have spaces in them. How do I handle that when replacing "Sheet1" and "Sheet2"? When I type without spaces, I get this error
View attachment 98026
You don't use the sheet names, you use the code names. Don't change the code as posted at all. You can change Sheet1 to anything you like (e.g. "Sheet 1 of 2") but the code name does not change.
 
Upvote 0
Have done as instructed. Data entered in Sheet1 D4 does not copy to Sheet2 E5. Have tried other cells. No result.
 
Upvote 0
So, basically, D19 should copy to E5, D20 to E6, D21 to E7, etc, on Sheet 2 if entered in Sheet 1 range. If entered in Sheet 2 range, should be E5 to D19 on Sheet 1, E6 to D20, E7 to D21 etc.
 
Upvote 0

Forum statistics

Threads
1,216,094
Messages
6,128,785
Members
449,468
Latest member
AGreen17

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