Solved

Copy and paste data between workbooks using VBA

Posted on 2013-12-10
4
3,238 Views
Last Modified: 2013-12-10
Dear Experts:

below code ...
... prompts the user to open an excel file (source file)
... copies the data from the first worksheet of the source file
... pastes this data into the first worksheet of the currently active workbook (target file)

I would like somebody to tweak this code so that ...
(1) '/// The targetSheet is to be inserted as a separate sheet right at the very beginning.
(2) '/// Everything has to be copied, i.e. values, formulas, column and row widths
(3) '/// If a sheet with the same name already exists in the target workbook, a Msgbox has to say so and the macro is to abort!

Help is much appreciated. Thank you very much in advance.

Regards, Andreas


Private Sub Import_Data_Into_Current_Workbook()
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

' active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook

With Application.fileDialog(msoFileDialogOpen)

    .ButtonName = "&Open"
    .InitialFileName = "C:\MyFile\Data"
    .Filters.Clear
    .Filters.Add "Excel files (*.xls; *.xlsm; *.xlsx)", "*.xls;*.xlsm;*.xlsx", 1
    .Title = "Please Select an input file"
    .AllowMultiSelect = False
End With

  If Not (Application.fileDialog(msoFileDialogOpen).Show) Then
     MsgBox "No File specified!.", vbExclamation, "Cancel has been pressed!"
     Exit Sub
  Else
     customerFilename = Application.fileDialog(msoFileDialogOpen).SelectedItems(1)

Set customerWorkbook = Application.Workbooks.Open(customerFilename)

' copy data from customer to target workbook
'/// to be incorporated (1) The targetSheet is to be inserted as a separate sheet right at the very beginning.
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)

'/// To be incorporated (2): Everything has to be copied, i.e. values, formulas, column and row widths
Intersect(sourceSheet.UsedRange, sourceSheet.UsedRange.Offset(0, 0)).Copy targetSheet.Range("A1")
'/// To be incorporated (3): If a sheet with the same name already exists in the target workbook, a msgbox has to say so and the macro is to abort!
targetSheet.Name = sourceSheet.Name

  End If

customerWorkbook.Close

End Sub

Open in new window

0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 250 total points
ID: 39708534
Try replacing

Intersect(sourceSheet.UsedRange, sourceSheet.UsedRange.Offset(0, 0)).Copy targetSheet.Range("A1")

with

sourceSheet.Copy before:=targetWorkbook.Sheets(1)
If targetWorkbook.Sheets(1).Name <> sourceSheet.Name Then
    targetWorkbook.Sheets(1).Delete
    MsgBox ("That name already exists in target workbook")
    Exit Sub
End If
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 39708620
Hi,

pls try

Private Sub Import_Data_Into_Current_Workbook()
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

' active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook

With Application.FileDialog(msoFileDialogOpen)

    .ButtonName = "&Open"
    .InitialFileName = "C:\MyFile\Data"
    .Filters.Clear
    .Filters.Add "Excel files (*.xls; *.xlsm; *.xlsx)", "*.xls;*.xlsm;*.xlsx", 1
    .Title = "Please Select an input file"
    .AllowMultiSelect = False
End With

  If Not (Application.FileDialog(msoFileDialogOpen).Show) Then
     MsgBox "No File specified!.", vbExclamation, "Cancel has been pressed!"
     Exit Sub
  Else
     customerFilename = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

Set customerWorkbook = Application.Workbooks.Open(customerFilename)


Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)

On Error Resume Next
Set srcSht = targetWorkbook.Sheets(sourceSheet.Name)
On Error GoTo 0
    If IsEmpty(srcSht) Then
    sourceSheet.Copy Before:=targetWorkbook.Sheets(1)
    Else
        MsgBox "Sheet name already there"
        Exit Sub

    End If
End If

customerWorkbook.Close

End Sub

Open in new window

Regards
0
 

Author Comment

by:AndreasHermle
ID: 39708635
Hi ssaqibh,

seems to work just great. I will do some more testing and then let you know.

Thank you very much for your great help.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 39709054
Dear both,

works just great, I can use both codes. Thank you very much for your great and professional help. Regards, Andreas
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

751 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