[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel VBA copy and paste between 2 files

Posted on 2010-01-07
5
Medium Priority
?
935 Views
Last Modified: 2013-12-20
HI,

Let's clarify the requirement :

Everytime open  database.xls , then  drag mouse left button to    highlight  some columns (every time different column)     , then  copy  highlighted columns  into  " D:\SUN_NGAI.xls ",   then close window of   database.xls .


The result show as  attached files
So what VB codes  should write for   database.xls  ?

Thx
database.xls
SUN-NGAI.xls
0
Comment
Question by:candychan611
  • 3
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
alainbryden earned 2000 total points
ID: 26204057
Click the "record a macro" button and then repeat your actions. The macro that appears in Module1 should be very close to what you need. Paste it here and let us know what additional functionality you need.

--
Alain
0
 

Author Comment

by:candychan611
ID: 26207137
Sub Macro1()
'
' Macro1 Macro
' Ahdee ( 2010/1/8 ý„èÆ
'

'
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("D:D").Select
    Selection.Cut
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight
    Columns("G:G").Select
    Selection.Cut
    Columns("E:E").Select
    ActiveSheet.Paste
    Range("A1:F2").Select
    Selection.Copy
    Application.WindowState = xlMinimized
    Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Windows("Book1").Activate
    ActiveWindow.Close
End Sub


---the marco cannot     paste that  sorted   data  to   D:\SUN_NGAI.xls  , can u  amend  the code ?
Thx
0
 

Author Comment

by:candychan611
ID: 26207460
Alain,

Pls ignore the above code,could you  amend  this one: Next step to  paste datas to  D:\SUN_NGAI.xls   CELL A6



Sub Macro2()
'
' Macro2 Macro
'
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight
    Selection.Insert Shift:=xlToRight
    Columns("F:F").Select
    Selection.Cut
    Columns("A:A").Select
    ActiveSheet.Paste
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight
    Columns("H:H").Select
    Selection.Cut
    Columns("E:E").Select
    ActiveSheet.Paste
    Range("A1:F2").Select
    Selection.Copy
End Sub
0
 
LVL 21

Assisted Solution

by:alainbryden
alainbryden earned 2000 total points
ID: 26209813
The macro should be able to record you pasting it into that workbook too. You have to open the workbook, paste the data, and then save and close it again. The macro should capture all those actions. You cannot paste anything into a closed workbook.

You eventually get code that looks a bit like this:

Workbooks.Open("D:\SUN_NGAI.xls")
Workbooks("SUN_NGAI.xls").Sheet1.Range("A6").Paste
Workbooks("SUN_NGAI.xls").Close True

--
Alain
0
 

Author Comment

by:candychan611
ID: 26240440
After modify  this is working for me :


Sub SUN_NGAI()

  Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight
    Selection.Insert Shift:=xlToRight
    Columns("F:F").Select
    Selection.Cut
    Columns("A:A").Select
    ActiveSheet.Paste
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight
    Columns("H:H").Select
    Selection.Cut
    Columns("E:E").Select
    ActiveSheet.Paste
     Range("A1:F53").Select
    ActiveWindow.SmallScroll Down:=-66
    Selection.Copy
   
    Workbooks.Open ("D:\.\SUN_NGAI.xls")
     Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question