Copy and paste data specified range between workbooks using VBA

hasan çapın
hasan çapın used Ask the Experts™
on
Dear Experts,

i need a code,

1- ... prompts the user to open an excel file (source file)
2-... copies the data from  Range(D3:E9999)  of the first worksheet of the source file
3- ... pastes this data into Range(A9:B9999) the first worksheet of the currently active workbook (target file)
4- If there ise data in Range(A9:B9999), pastes copied data into Range(C9:D99999)
5-If there ise data in Range(C9:D9999) too, pastes copied data into Range(E9:F99999)


Thank you very much for your great and professional help.

 Regards,
Hasan
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
Hi Hasan,

Try below:
Sub ImportData()
Dim SourceWB As Workbook, TargetWB As Workbook
Dim SourceWs As Worksheet, TargetWs As Worksheet
Dim FolderPath As String, Filter As String, Caption As String, SourceFName As String
Dim CopyRng As Range, PasteRng As Range
Dim LCol As Long

'Disable Events
With Application
    .ScreenUpdating = False
    .DisplayStatusBar = True
    .StatusBar = "!!! Please Be Patient...Uploading Data !!!"
    .EnableEvents = False
    .Calculation = xlManual
End With

'Define Variables
Set TargetWB = ThisWorkbook
Set TargetWs = TargetWB.Sheets(1)
FolderPath = Application.ThisWorkbook.Path
ChDir FolderPath
Filter = "Text files (*.xl*),*.xl*"
Caption = "Please Select an input file "
SourceFName = Application.GetOpenFilename(Filter, , Caption)
Set SourceWB = Application.Workbooks.Open(SourceFName, Format:=xlDelimited, Local:=True)
Set SourceWs = SourceWB.Sheets(1)
Set CopyRng = SourceWs.Range("D3:E9999")
LCol = TargetWs.Cells(9, Columns.Count).End(xlToLeft).Column
Set PasteRng = TargetWs.Range(TargetWs.Cells(9, LCol + 1), TargetWs.Cells(9, LCol + 1))

'Copy Data
If TargetWs.Range("A9") = "" Then
    CopyRng.Copy
    TargetWs.Range("A9").PasteSpecial xlPasteValuesAndNumberFormats
Else
    CopyRng.Copy
    PasteRng.PasteSpecial xlPasteValuesAndNumberFormats
End If

Application.CutCopyMode = False

'Close Source Workbook
Application.DisplayAlerts = False
SourceWB.Close SaveChanges:=False
Application.DisplayAlerts = True

TargetWs.Activate
TargetWs.Range("A8").Select

'Enable Events
With Application
    .ScreenUpdating = True
    .DisplayStatusBar = True
    .StatusBar = False
    .EnableEvents = True
    .Calculation = xlAutomatic
End With
End Sub

Open in new window


I have attached both sample for Target Workbook & Source Workbook.

Create a sample folder on your desktop and download both attached file in that folder, open TargetWB and try multiple times.

Hope this helps.
SourceWB.xlsx
TargetWB.xlsm

Author

Commented:
First of all, thank you for your attention.
 
Code is working good. However i have a problem, which is not appearing in your example Source WB. xlsx.

With same code, in my source file i can take first data correctly but at the second data, data is pasted   into Range(Q9:R99999)

As i said that, this problem is not occuring in your example.

What is difference could you help me again?
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
Could you upload your target workbook?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
You can find at the attached file
250C_datagiri-.xlsm
Managing Director/Excel VBA Developer
Distinguished Expert 2018
Commented:
Hi Hasan,

Try below:
Sub ImportData()
Dim SourceWB As Workbook, TargetWB As Workbook
Dim SourceWs As Worksheet, TargetWs As Worksheet
Dim FolderPath As String, Filter As String, Caption As String, SourceFName As String
Dim CopyRng As Range, PasteRng As Range

'Disable Events
With Application
    .ScreenUpdating = False
    .DisplayStatusBar = True
    .StatusBar = "!!! Please Be Patient...Uploading Data !!!"
    .EnableEvents = False
    .Calculation = xlManual
End With

'Define Variables
Set TargetWB = ThisWorkbook
Set TargetWs = TargetWB.Sheets(1)
FolderPath = Application.ThisWorkbook.Path
ChDir FolderPath
Filter = "Text files (*.xl*),*.xl*"
Caption = "Please Select an input file "
SourceFName = Application.GetOpenFilename(Filter, , Caption)
Set SourceWB = Application.Workbooks.Open(SourceFName, Format:=xlDelimited, Local:=True)
Set SourceWs = SourceWB.Sheets(1)
Set CopyRng = SourceWs.Range("D3:E9999")
Set PasteRng = TargetWs.Range("A9").End(xlToRight).Offset(, 1)

'Cancel Exit
If IsEmpty(SourceFName) Then Exit Sub

'Copy Data
If TargetWs.Range("A9") = "" Then
    CopyRng.Copy
    TargetWs.Range("A9").PasteSpecial xlPasteValuesAndNumberFormats
Else
    CopyRng.Copy
    PasteRng.PasteSpecial xlPasteValuesAndNumberFormats
End If

Application.CutCopyMode = False

'Close Source Workbook
Application.DisplayAlerts = False
SourceWB.Close SaveChanges:=False
Application.DisplayAlerts = True

TargetWs.Activate
TargetWs.Range("A8").Select

'Enable Events
With Application
    .ScreenUpdating = True
    .DisplayStatusBar = True
    .StatusBar = False
    .EnableEvents = True
    .Calculation = xlAutomatic
End With
End Sub

Open in new window


Attached is your file with edited code.
250C_datagiri-.xlsm
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
No Comments added after last solution, considered as accepted solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial