PDA

View Full Version : Is there a way to shorten these codes?



doctortt
05-06-2011, 01:28 PM
Hi gurus,

can someone help me to take a look at these codes and see if there is a way to shorten them? I'm a newbie =(

OpenFile = Application.GetOpenFilename(FileFilter:="Excel files(*.xls), *.xls")
Workbooks.Open(OpenFile).Sheets("info").Cells.Copy
ActiveWindow.Close
Workbooks("abc.xls").Worksheets("test").Activate
Worksheets("test").Cells.Select
Worksheets("test").Paste

macropod
05-06-2011, 11:57 PM
Hi doctortt,

Try:

Dim OpenFile As String
OpenFile = Application.GetOpenFilename(FileFilter:="Excel files(*.xls), *.xls")
If OpenFile = False Then Exit Sub
Workbooks.Open(OpenFile).Sheets("info").Cells.Copy
Workbooks("abc.xls").Worksheets("test").Range("A1").Paste

Chabu
05-08-2011, 02:49 PM
The VBA help is your best friend! Range.copy has a destination parameter

Application.ActiveWorkbook.ActiveSheet.Cells.Copy Destination:=ActiveWorkbook.Worksheets("copy").Cells

macropod
05-08-2011, 03:21 PM
Hi Chabu,

The OP's code suggests the source and destination workbooks are different and, with the in-line approach, the paste won't be going to the ActiveWorkbook either, so:
Workbooks.Open(OpenFile).Sheets("info").Cells.Copy _
Destination:=Workbooks("abc.xls").Worksheets("test").Cells