Excel VBA - copy and paste changing values from one worksheet to another

Ceslior

New Member
Joined
Jan 27, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hey, I'm new to VBA and this forum and wanted to ask for help.

I have two worksheets; "Report" and "Total".
The goal is to save the values of my table in my "Total" sheet based on the calendar week in cell "S8" as well as 'load' the data as soon as I select the calendar week in cell "S8"

Hopefully there is someone who can adjust my code, so it's not flashing every time I change a value.

Thanks in advance!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Target, Range("C2:D8")) Is Nothing Then
    
    Range("C4:D4").Copy
    CW = Sheets("Report").Range("S8")
    
    Sheets("Total").Select
    Sheets("Total").Range("1:1").Select
    Selection.Find(What:=CW, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, 1).Select
    ActiveCell.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
    Sheets("Report").Select
    End If
    
    If Not Intersect(Target, Range("S8")) Is Nothing Then
    CW = Sheets("Report").Range("S8")
    
    Sheets("Total").Select
    Sheets("Total").Range("1:1").Select
    Selection.Find(What:=CW, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(, 1).Resize(1, 2).Copy
    Sheets("Report").Select
    Sheets("Report").Range("C2:D8").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
    End If
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Can you step run your code using F8 and see where is it wrong, maybe the IF conditions are not coming to be true?
Step run your code while different sheets are active, maybe that is part of the problem.
 
Upvote 0
Can you step run your code using F8 and see where is it wrong, maybe the IF conditions are not coming to be true?
Step run your code while different sheets are active, maybe that is part of the problem.
I apologise for the late response. I worked a bit on my code and it's working as intended now.
My only problem now is, I have 3 tables in my first sheet "Report" and one in "Report2". Do I have to create a bunch of IFs or am I able to put them together?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
'Save changing values
    
    If Not Intersect(Target, Range("C4:D4")) Is Nothing Then
    
    Range("C4:D4").Copy
    KW = Sheets("Report").Range("S8")
    
    Sheets("Total").Visible = True
    Sheets("Total").Select
    Sheets("Total").Range("1:1").Select
    
    Selection.Find(What:=KW, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Offset(0, 1).PasteSpecial xlPasteValues
    
    Application.CutCopyMode = False
    Sheets("Total").Cells(1, 1).Select
    Sheets("Total").Visible = xlSheetVeryHidden
    Sheets("Report").Select
    
    End If
    
'Load all values

    If Not Intersect(Target, Range("S8")) Is Nothing Then
    KW = Sheets("Report").Range("S8")
    
    Sheets("Total").Visible = True
    Sheets("Total").Select
    Sheets("Total").Range("1:1").Select
    
    Selection.Find(What:=KW, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Offset(, 1).Resize(1, 2).Copy
    
    Sheets("Report").Range("C4:D4").PasteSpecial Paste:=xlPasteValues
    Cells(1, 1).Select
    Application.CutCopyMode = False
    
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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