VBA overwrite table in sheet1 with table in sheet2

RandomUserCode

New Member
Joined
Aug 4, 2021
Messages
26
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a schedule in sheet1 and a schedule in sheet2. I want to "subtract" the table range in sheet2 with the table range in sheet1. Sheet2 is the "skeleton" of the schedule, and the determined values in specific cells. Sheet1 is the "skeleton table", just with cells added afterwards. So i want to reset the table in sheet1, to its "skeleton", which is seen in sheet2.

Made this code, but cant make it work:

VBA Code:
Function RangeMinus(r1 As Range, r2 As Range) As Range

Dim rCell As Range

For Each rCell In r1
    If Intersect(rCell, r2) Is Nothing Then
        If RangeMinus Is Nothing Then
            Set RangeMinus = rCell
        Else
            Set RangeMinus = Application.Union(RangeMinus, rCell)
        End If
    End If
Next rCell

End Function

VBA Code:
Sub test()

    Dim rMinus As Range

    Set rMinus = RangeMinus(Sheets(2).Range("B2:F10"), Sheets(1).Range("B2:F10"))
    MsgBox rMinus.Address(0, 0)

End Sub

The schedule tables looks like this:
 

Attachments

  • Sheet1.PNG
    Sheet1.PNG
    10.3 KB · Views: 11
  • Sheet2.PNG
    Sheet2.PNG
    10.4 KB · Views: 10

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
A gentle reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
A gentle reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
My bad, will just paste the link if i cross-post a question some other time, TY
 
Upvote 0
I have a schedule in sheet1 and a schedule in sheet2. I want to "subtract" the table range in sheet2 with the table range in sheet1. Sheet2 is the "skeleton" of the schedule, and the determined values in specific cells. Sheet1 is the "skeleton table", just with cells added afterwards. So i want to reset the table in sheet1, to its "skeleton", which is seen in sheet2.

Made this code, but cant make it work:

VBA Code:
Function RangeMinus(r1 As Range, r2 As Range) As Range

Dim rCell As Range

For Each rCell In r1
    If Intersect(rCell, r2) Is Nothing Then
        If RangeMinus Is Nothing Then
            Set RangeMinus = rCell
        Else
            Set RangeMinus = Application.Union(RangeMinus, rCell)
        End If
    End If
Next rCell

End Function

VBA Code:
Sub test()

    Dim rMinus As Range

    Set rMinus = RangeMinus(Sheets(2).Range("B2:F10"), Sheets(1).Range("B2:F10"))
    MsgBox rMinus.Address(0, 0)

End Sub

The schedule tables looks like this:
Error message can be seen in this picture:
error table.PNG
 
Upvote 0
If r1 and r2 are on different sheets, why are you using Intersect (which won't work) when they can't possibly overlap?
 
Upvote 0
If r1 and r2 are on different sheets, why are you using Intersect (which won't work) when they can't possibly overlap?
I tried in one sheet, but changed it to two different sheets. Dont know how to do it in two different sheets
 
Upvote 0
I don't understand how this code relates in any way to the described situation. It sounds like all you need to do is copy the skeleton table over the table on sheet1.
 
Upvote 0
I don't understand how this code relates in any way to the described situation. It sounds like all you need to do is copy the skeleton table over the table on sheet1.
Yeah that is about right. I want to copy sheet2 and overwrite sheet1 (in the same range) on a specific time/day. Can it be made easier than the code prevented?
 
Upvote 0
Yes:

Code:
Sheets(2).Range("B2:F10") Sheets(1).Range("B2")
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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