Shorten VBA Code / Better way to write it

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,516
Office Version
  1. 2016
Platform
  1. Windows
Hi All,


Is there a way to shorten the below code...


Code:
Sub Macro4()'
' Macro4 Macro
'


'
    Range("B3").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("B11").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
End Sub


Regards,

Humayun
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Possibly:

Rich (BB code):
   With Range("B3").CurrentRegion
        .Copy Range("B11").Resize(.Rows.Count, .Columns.Count)
    End With

Though it depends on your data layout
 
Upvote 0
Hi,

You can test

Code:
Sub Macro4()
    Range("B3").CurrentRegion.Copy Destination:=Range("B11")
End Sub

Hope this will help
 
Upvote 0
Hello,

Thanks Kyle & james,

Both working perfect.

James i am gonna go with your one as it's only a single line :)
 
Upvote 0
two additions in the below code please :)

Range("B3").CurrentRegion.Copy Destination:=Range("B11")

1) if the file is another sheet - lets say sheet2
2) if the file is another folder - lets say d: drive folder:humayun file name:rayani sheet2
 
Upvote 0
i mean if the destination file is on another sheet or folder
 
Upvote 0
Hello again,

You need to explicit the destination ...

Both workbooks need to opened ...


Range("B3").CurrentRegion.Copy Destination:=Workbooks("rayani.xlsm").Sheet2.Range("B11")

Hope this will help

 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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