Link to home
Create AccountLog in
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.

Avatar of Shums Faruk
Shums Faruk
Flag of India image

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
    TargetWs.Range("A9").PasteSpecial xlPasteValuesAndNumberFormats
    PasteRng.PasteSpecial xlPasteValuesAndNumberFormats
End If

Application.CutCopyMode = False

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


'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.
Avatar of hasan çapın
hasan çapın


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?
Could you upload your target workbook?
You can find at the attached file
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
No Comments added after last solution, considered as accepted solution