We help IT Professionals succeed at work.

Copy and paste data specified range between workbooks using VBA

158 Views
Last Modified: 2017-11-20
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

ShumsManaging Director/Excel VBA Developer
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Could you upload your target workbook?

Author

Commented:
You can find at the attached file
250C_datagiri-.xlsm
Managing Director/Excel VBA Developer
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
ShumsManaging Director/Excel VBA Developer
CERTIFIED EXPERT
Distinguished Expert 2018

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions