'Excel: How do I copy and paste ranges into different cells/worksheets based on the real world date?
I have a number worksheets Current Standing, 2022, 2023, 2024 etc
Current standing is just a single range D12:D35 and 2022/3/4 look like this:
I'm trying to copy the range D12:D35 from Current Standing to the current month & year. Current month and year being the literal current real world date.
If I ran the script today it should:
- Copy
D12:D35fromCurrent Standing - Paste it into
D3/Aprilin the2022worksheet.
If I run the script in 3 months (July):
- Copy
D12:D35fromCurrent standing - Paste it into
G3/Julyin the 2022 worksheet.
If I run the script in 12 months (April, 2023):
- Copy
D12:D35fromCurrent standing - Paste it into
D3/Aprilin the 2023 worksheet.
I've managed to figure out how to copy and paste with vba, while protecting and unprotecting the sheet but I'm not sure how I'd take account of the month and year requirement.
Option Explicit
Sub update()
If Worksheets("Current Standing").ProtectContents = True Then
Worksheets("Current Standing").Unprotect
Range("D12:D35").Copy
Range("F12:F35").PasteSpecial xlPasteValues
Worksheets("Current Standing").Protect
Else
Range("D12:D35").Copy
Range("F12:F35").PasteSpecial xlPasteValues
Range("F12:F35").PasteSpecial xlPasteValues
Worksheets("Current Standing").Protect
End If
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

