VBA - Paste data dynamically in masterworkbook from different workbooks

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,


please help, My task is to open each workbooks from a range, copy workbooks range and paste in master file,
But Challenge I am facing is that firstworkbooks data will be pasted correctly. but second workbooks
Data should be pasted in specified range of Master workbook.and row should be adjusted.


I think it needs to check workbooks name and jump to that correct range


Thanks in advance for precious time and help.

Code:
Option Explicit
Sub Test()
    Dim i As Integer
    Dim wbk As Workbook
    Dim mwbk As Workbook
    Dim inputsheet As Worksheet
    Dim outputsheet As Worksheet
    Dim path As String
    Dim lr As Long
    
    Set mwbk = ThisWorkbook
    Set inputsheet = mwbk.Worksheets("Input")
    Set outputsheet = wbk.Worksheets("MasterFile")
    
    path = "E:\"
    
    For i = 6 To 9
        On Error Resume Next
        Set wbk = Workbooks.Open(inputsheet.Range("b6").Value, ReadOnly:=True)
        On Error GoTo 0
        
        'if wbk.Name = Team1 then do this activity
        'if wbk.Name = Team2 then jump2 Team2 steps
        
        Set Datasheet = wbk.Worksheets("Sheet1") 'My data sheet to copy the data
        
        If wbk Is Nothing Then Exit Sub   'Plz assist is this line correct
                If Not wbk Is Nothing Then   ' if wbk found continue
                      lr = Datasheet.Range("a65555").End(xlUp).Row
                        If lr < 2 Then GoTo closeworkbook
                        'if lr is 15 row then i have insert extra row   ****** Challenges*******
                                'and format should also maintain as it it
                        Datasheet.Range("a1").currenregion.Copy
                        outputsheet.Range("b4").PasteSpecial xlPasteAll
                End If
        End If
        
    GoTo closeworkbook:
    wbk.Close False
    Next i
    
End Sub


Regards,
Mallesh
 
Last edited by a moderator:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top