Solved

Excel VBA to copy range from closed workbook to active workbook

Posted on 2016-07-21
2
80 Views
Last Modified: 2016-07-27
Looking to use VBA to copy range columns A:W from a closed workbook in worksheet "Worksheet1" to my active workbook, worksheet "Worksheet1" range  columns A:W

The source workbook has a different name each time it is looked for, as it is renamed after the last update.
0
Comment
Question by:DougDodge
[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 Comments
 
LVL 31

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41723194
You may try something like this....
The code will prompt you to select the source file.

Sub CopyFromAnotherWorkbook()
Dim swb As Workbook, wb As Workbook
Dim sws As Worksheet, ws As Worksheet

Dim SelectedFile As String

Application.ScreenUpdating = False
Set swb = ThisWorkbook
Set sws = swb.Sheets("Sheet1")
sws.Range("A:W").ClearContents

With Application.FileDialog(msoFileDialogFilePicker)
   .Title = "Select A Folder!"
   .ButtonName = "Confirm"
   .Filters.Clear
   .Filters.Add "Select an Excel File to open", "*.xlsx"
   If .Show = -1 Then
      SelectedFile = .SelectedItems(1)
   Else
      MsgBox "You didn't select a file.", vbExclamation, "File Not Selected!"
      Exit Sub
   End If
End With
Workbooks.Open SelectedFile
Set wb = ActiveWorkbook
Set ws = wb.Sheets(1)
ws.Range("A:W").Copy sws.Range("A1")
wb.Close False
Application.ScreenUpdating = True
MsgBox "Data has been successfully copied.", vbInformation, "Done!"
End Sub

Open in new window

0
 
LVL 19

Expert Comment

by:Roy_Cox
ID: 41723442
If you don't want to open the workbook there's some code eamples using ADO here
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

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