Hi all,
I am looking to import a sheet into an opened workbook. I have found a code from this URL http://www.erlandsendata.no/english/index.php?d=envbatextimportwb , but I can't manage to get this done correctly. I want to copy the range "A1:Z1000" from the "Source" sheet to the target sheet "Div_P&L" (which will be opened when the macro is run) located in another folder.
Source address : "H:\P&L\YE Temp\Div P&L\P&L Report 020312.xls" , sheet("Source"), data = range("A1:Z1000").
Target address : "H:\Yield Enhancement\PandL.xls" , sheet("Div_P&L") , data = range ("A1")
Line Workbooks(PandL).Activate => Run time error 9, Subscript out of range.
Has anyone any idea ?
I have tried the getvalue function which works, but it takes a long time. So I want to use an ADO way.
Thanks a lot
I am looking to import a sheet into an opened workbook. I have found a code from this URL http://www.erlandsendata.no/english/index.php?d=envbatextimportwb , but I can't manage to get this done correctly. I want to copy the range "A1:Z1000" from the "Source" sheet to the target sheet "Div_P&L" (which will be opened when the macro is run) located in another folder.
Source address : "H:\P&L\YE Temp\Div P&L\P&L Report 020312.xls" , sheet("Source"), data = range("A1:Z1000").
Target address : "H:\Yield Enhancement\PandL.xls" , sheet("Div_P&L") , data = range ("A1")
Line Workbooks(PandL).Activate => Run time error 9, Subscript out of range.
Has anyone any idea ?
I have tried the getvalue function which works, but it takes a long time. So I want to use an ADO way.
Thanks a lot
Code:
Sub importdata()
ImportRangeFromWB "H:\P&L\YE Temp\Div P&L\P&L Report 020312.xls", "Source", "A1:Z1000", True, "PandL.xls", "Div_P&L", "A1"
End Sub
Code:
Sub ImportRangeFromWB(SourceFile As String, SourceSheet As String, _
SourceAddress As String, PasteValuesOnly As Boolean, _
TargetWB As String, TargetWS As String, TargetAddress As String)
' Imports the data in Workbooks(SourceFile).Worksheets(SourceSheet).Range(SourceAddress)
' to Workbooks(TargetWB).Worksheets(TargetWS).Range(TargetAddress)
' Replaces existing data in Workbooks(TargetWB).Worksheets(TargetWS)
' without prompting for confirmation
' Example
' ImportRangeFromWB "C:\FolderName\TargetWB.xls", _
"Sheet1", "A1:E21", True, _
ThisWorkbook.Name, "ImportSheet", "A3"
Dim SourceWB As Workbook, SourceWS As String, SourceRange As Range
Dim TargetRange As Range, A As Integer, tString As String
Dim r As Long, c As Integer
' validate the input data if necessary
If Dir(SourceFile) = "" Then Exit Sub ' SourceFile doesn't exist
Set SourceWB = Workbooks.Open(SourceFile, True, True)
Application.StatusBar = "Reading data from " & SourceFile
Workbooks(PandL).Activate
Worksheets("Div_P&L").Activate
' perform import
Set TargetRange = Range(TargetAddress).Cells(1, 1)
Set SourceRange = SourceWB.Worksheets(SourceSheet).Range(SourceAddress)
For A = 1 To SourceRange.Areas.Count
SourceRange.Areas(A).copy
If PasteValuesOnly Then
TargetRange.PasteSpecial xlPasteValues
TargetRange.PasteSpecial xlPasteFormats
Else
TargetRange.PasteSpecial xlPasteAll
End If
Application.CutCopyMode = False
If SourceRange.Areas.Count > 1 Then
Set TargetRange = _
TargetRange.offset(SourceRange.Areas(A).Rows.Count, 0)
End If
Next A
' clean up
Set SourceRange = Nothing
Set TargetRange = Nothing
Range(TargetAddress).Cells(1, 1).Select
SourceWB.Close False
Set SourceWB = Nothing
Application.StatusBar = False
End Sub