PDA

View Full Version : Import data from closed WB error



sujittalukde
11-20-2008, 06:10 AM
I am using the following code to import data from closed workbook but an error is coming.


Sub Import_MasterData()
Dim maswb As Workbook
Dim maspath As String
Dim x As Integer
Dim Lx As Integer
Application.ScreenUpdating = False
maspath = ThisWorkbook.path
Set maswb = Workbooks.Open(maspath & "\Master_Employee_File.xls", True, True)
'Set start row
x = 2
Lx = maswb.Worksheets("MasterData").Cells(Rows.Count, 2).End(xlUp).Row
'With ThisWorkbook.Worksheets("MasterData")
maswb.Worksheets("MasterData").Rows("x:Lx").Copy Destination:=ThisWorkbook.Worksheets("MasterData").Range("A2")
maswb.Close False
Set maswb = Nothing
Application.ScreenUpdating = True
End Sub


Error is coming for this line:



maswb.Worksheets("MasterData").Rows("x:Lx").Copy Destination:=ThisWorkbook.Worksheets("MasterData").Range("A2")


The error is :
Run-time error 1004
Application defined or Object defined error

How can I rectify it?

Kenneth Hobs
11-20-2008, 06:30 AM
Dim lx as Long
maswb.Worksheets("MasterData").Rows(x & ":" & Lx).Copy Destination:=ThisWorkbook.Worksheets("MasterData").Range("A2")