Avatar of hasan çapın
hasan çapın
 asked on

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
Microsoft OfficeVBAMicrosoft ExcelMicrosoft ApplicationsProgramming

Avatar of undefined
Last Comment
Shums Faruk

8/22/2022 - Mon
Shums Faruk

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
hasan çapın

ASKER
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?
Shums Faruk

Could you upload your target workbook?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
hasan çapın

ASKER
You can find at the attached file
250C_datagiri-.xlsm
ASKER CERTIFIED SOLUTION
Shums Faruk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Shums Faruk

No Comments added after last solution, considered as accepted solution