Copy and paste data specified range between workbooks using VBA

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
hasan çapınAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShumsDistinguished Expert - 2017Commented:
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
0
hasan çapınAuthor 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?
0
ShumsDistinguished Expert - 2017Commented:
Could you upload your target workbook?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

hasan çapınAuthor Commented:
You can find at the attached file
250C_datagiri-.xlsm
0
ShumsDistinguished Expert - 2017Commented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShumsDistinguished Expert - 2017Commented:
No Comments added after last solution, considered as accepted solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.