Tidy up simple code?

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,066
Office Version
  1. 365
Platform
  1. Windows
I have this;

Windows("file1.xlsx").Activate
Range("F4:F500").Select
Application.CutCopyMode = False
Selection.Copy
Windows("file2.xlsm").Activate
Range("L2").Select
ActiveSheet.Paste

I'm sure this can be reduced somewhat - any suggestions?

Thanks :-)
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I have this;

Windows("file1.xlsx").Activate
Range("F4:F500").Select
Application.CutCopyMode = False
Selection.Copy
Windows("file2.xlsm").Activate
Range("L2").Select
ActiveSheet.Paste

I'm sure this can be reduced somewhat - any suggestions?

Thanks :-)

This assumes that the data is copied from Sheet1 and pasted to Sheet1, you can change these as appropriate.

VBA Code:
Public Sub subTidyUpCode()
  
  Workbooks("file1.xlsx").Worksheets("Sheet1").Range("F4:F500").Copy Workbooks("file2.xlsm").Worksheets("Sheet1").Range("L2")
    
End Sub
 
Upvote 0
Solution
Many thanks - worked perfectly, and so much tidier :)
 
Upvote 0
How could I amend your code to just paste special values instead?
 
Upvote 0
Just pasting the raw values, not the formats, formulae etc
 
Upvote 0
Try
VBA Code:
Sub Copy_Values()
  Workbooks("file2.xlsm").Worksheets("Sheet1").Range("L2:L498").Value = Workbooks("file1.xlsx").Worksheets("Sheet1").Range("F4:F500").Value
End Sub
 
Upvote 0
An alternative would be

VBA Code:
Sub Copy_Values_v2()
  Workbooks("file1.xlsx").Worksheets("Sheet1").Range("F4:F500").Copy
  Workbooks("file2.xlsm").Worksheets("Sheet1").Range("L2").PasteSpecial xlValues
  Application.CutCopyMode = False
End Sub
 
Upvote 0
Just pasting the raw values, not the formats, formulae etc
VBA Code:
Public Sub subTidyUpCode()

  Workbooks("file1.xlsx").Worksheets("Sheet1").Range("F4:F500").Copy
  
  Workbooks("file2.xlsm").Worksheets("Sheet1").Range("L2").PasteSpecial (xlPasteValues)

  Workbooks("file2.xlsm").Activate
  
  Range("L2").Select
  
  Application.CutCopyMode = False
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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