VBA code without selecting sheets

Afro_Cookie

Board Regular
Joined
Mar 17, 2020
Messages
103
Office Version
  1. 365
Platform
  1. Windows
Is it possible to run code on sheets without selecting them before hand? Below are two examples. The top version works, but the bottom does not.

VBA Code:
'This works    
    Sheets("Today").Select
    ActiveSheets.Range("A1").CurrentRegion.Offset(1, 0).Select
    Selection.Cut
    Sheets("Yesterday").select
    ActiveSheets.Range("A2").Paste

'This does not work
    Sheets("Today").Range("A1").CurrentRegion.Offset(1, 0).Select
    Selection.Cut
    Sheets("Yesterday").Range("A2").Paste
 

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,)
Try it like
VBA Code:
    Sheets("Today").Range("A1").CurrentRegion.Offset(1, 0).Cut
    Sheets("Yesterday").Range("A2").PasteSpecial
 
Upvote 0
try

VBA Code:
    Dim rng As Range
    Set rng = Sheets("Today").Range("A1").CurrentRegion.Offset(1, 0)
    rng.Cut Sheets("Yesterday").Range("A2")

Dave
 
Upvote 0
Try it like
VBA Code:
    Sheets("Today").Range("A1").CurrentRegion.Offset(1, 0).Cut
    Sheets("Yesterday").Range("A2").PasteSpecial
Thanks for this Fluff, it did generate a run-time error

VBA Code:
Run-time error '1004'
PasteSpecial method of Range class failed

I tried dmt32's and it worked well.
 
Upvote 0
Yup, my code should have been
VBA Code:
    Sheets("Today").Range("A1").CurrentRegion.Offset(1, 0).Cut   Sheets("Yesterday").Range("A2")
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Glad we could help & thanks for the feedback.
I could use some help refining my code if you're willing.

I want to do something similar to what you helped with above, but I have formatting on the cells I want to copy. Is there a way to remove the 'select' function but still be able to 'PasteSpecial'?
VBA Code:
'Current code
ActiveSheet.Range("L6", ActiveSheet.Range("L6").End(xlDown).End(xlToRight)).Select
    Selection.Copy
    Sheets("Today").Select
    Range("K2").Select
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

'desired/attempted streamline
ActiveSheet.Range("L6", ActiveSheet.Range("L6").End(xlDown).End(xlToRight)).Copy Sheets("Today").Range("K2")
    PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
 
Upvote 0

Forum statistics

Threads
1,202,916
Messages
6,052,541
Members
444,591
Latest member
exceldummy774

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