Best way to get data from a closed workbook

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Usually use code in TestCopy but then recently saw testCopy, so was wondering which is the best way

Also in the first version if it contains a password you can use that in the code but wasn't sure how to use in the Get Object version

VBA Code:
Sub testCopy()
Dim wb As Workbook

Application.DisplayAlerts = False

Set wb = Workbooks.Open("C:\MyDocs\Test.xls", Password:="myPassword", ReadOnly:=True)

wb.Sheets("Sheet2").Range("A2:F100").Copy
ThisWorkbook.Sheets("Sheet1").Range("A2").PasteSpecial xlAll

wb.Close False
Application.DisplayAlerts = True

End Sub

and

VBA Code:
Sub testCopy2()
With GetObject("C:\MyDocs\Test.xls")
  .Sheets("Sheet2").Range("A2:F100").Copy ThisWorkbook.Sheets("Sheet1").Range("A2")
  .Close 0
End With

End Sub
 
But how do you get current region of closed workbook into a string using getdata?
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
But how do you get current region of closed workbook into a string using getdata?
Does anyone know how to get current region using get data instead of
VBA Code:
.Sheets("Sheet2").Range("A2:F100").Copy ThisWorkbook.Sheets("Sheet1").Range("A2")

and also how to use if the workbook has password (password is myPassword)
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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