+ Reply to Thread
Results 1 to 6 of 6

VBA to copy paste to specific worksheet from another workbook of same worksheet name

  1. #1
    Registered User
    Join Date
    07-16-2013
    Location
    Chennai
    MS-Off Ver
    Excel 2007
    Posts
    5

    Post VBA to copy paste to specific worksheet from another workbook of same worksheet name

    Hi Excel Gurus

    I have a 'Master.xlsm' file having worksheets like SLA1, SLA2....SLA36. Another file 'Source.xlsx' having SLA1, SLA2...so on but not all 36 sheets. I am looking for a VBA code to copy and paste the data from 'Source' to 'Master' based on same worksheet name. Previous data from A2 in 'Master' have to be delete before pasting the data.

    Thank you in advance

    Saleem Basha

  2. #2
    Registered User
    Join Date
    08-05-2015
    Location
    Finland
    MS-Off Ver
    Home and Business 2013
    Posts
    50

    Re: VBA to copy paste to specific worksheet from another workbook of same worksheet name

    Hello Saleem Basha, the following code will copy whole sheets from Source to Master. You will have to change number 3 to last "SLA" number in Souce. Let me know if something needs to be changed. Also Source file must be open.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to copy paste to specific worksheet from another workbook of same worksheet name

    Maybe:

    Please Login or Register  to view this content.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: VBA to copy paste to specific worksheet from another workbook of same worksheet name

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-16-2013
    Location
    Chennai
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: VBA to copy paste to specific worksheet from another workbook of same worksheet name

    Hi Greg

    Thank you. I modified the code according to my requirement. If previous data in 'Master' (Col A is date) is same as data in 'Source' (Col A is date), can we skip code for that sheet and continue the rest. For example, I have June data in Master file and now I am copying July data from Source file. If Master file sheet already exits July data, code can skip that sheet and run code rest of sheets.

    Highly appreciated your help.

    Sub Saleem_Basha()
    Application.ScreenUpdating = False
    Dim x As Long
    Dim MasterWb As String, SourceWb As String
    On Error Resume Next
    MasterWb = ActiveWorkbook.Name
    sFileName = Application.GetOpenFilename
    If sFileName = False Then
    Exit Sub
    Else
    Workbooks.Open Filename:=sFileName
    End If
    arr = Split(sFileName, "\")
    SourceWb = arr(UBound(arr))
    'SourceWb = Workbooks("Source1.xlsx").Name
    For x = 1 To 36 ' CHANGE 3 TO LAST SHEET SLA NUMBER IN SOURCE WORKBOOK

    Workbooks(MasterWb).Sheets("SLA" & x).UsedRange.Clear
    Workbooks(SourceWb).Sheets("SLA" & x).UsedRange.Copy

    ActiveSheet.Paste Destination:=Workbooks(MasterWb).Sheets("SLA" & x).Cells(1, 1)
    Application.CutCopyMode = False
    Next x

    Application.ScreenUpdating = True
    MsgBox "All done"

    End Sub

  6. #6
    Registered User
    Join Date
    07-16-2013
    Location
    Chennai
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: VBA to copy paste to specific worksheet from another workbook of same worksheet name

    I ended with the below code... Thank you

    Sub CopySameSht()
    Dim MasterWb As Workbook, SourceWb As Workbook
    Dim sht As Worksheet

    Application.ScreenUpdating = False
    'On Error Resume Next
    MyFileName = Application.GetOpenFilename(Title:="Please Choose Master File", _
    FileFilter:="Excel Files *.xls* (*.xls*),")
    Set MasterWb = Workbooks.Open(Filename:=MyFileName)

    MyFileName1 = Application.GetOpenFilename(Title:="Please Choose Source File", _
    FileFilter:="Excel Files *.xls* (*.xls*),")
    Set SourceWb = Workbooks.Open(Filename:=MyFileName1)

    For Each sht In SourceWb.Worksheets
    If SheetExistsInWorkbook(sht.Name, MasterWb) Then
    On Error Resume Next
    SourceWb.Sheets(sht.Name).ListObjects(1).Unlist
    With SourceWb.Sheets(sht.Name).UsedRange
    .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).Copy
    With MasterWb.Sheets(sht.Name).Cells(2, 1)
    .PasteSpecial xlPasteFormats
    .PasteSpecial xlPasteValues
    End With
    End With
    Application.CutCopyMode = False
    End If
    Next sht
    Application.ScreenUpdating = True
    MsgBox "All Done"
    End Sub
    Function SheetExistsInWorkbook(strShtName As String, wb As Workbook) As Boolean
    Dim sht As Worksheet

    For Each sht In wb.Sheets
    If sht.Name = strShtName Then
    SheetExistsInWorkbook = True
    Exit Function
    End If
    Next

    SheetExistsInWorkbook = False

    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to Copy/Paste History in specific worksheet, password protect workbook
    By gaker10 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2015, 02:58 PM
  2. Replies: 8
    Last Post: 10-27-2014, 11:27 PM
  3. Copy a row from master sheet and paste it to a specific worksheet based on tab name
    By obrie499 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-31-2013, 10:45 AM
  4. Open Multiple Files, Copy Worksheet, Paste in to Specific Tab in Original Workbook
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-30-2013, 10:27 AM
  5. Copy several columns and paste into specific column on another worksheet
    By lcy_84 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-31-2012, 09:19 AM
  6. Replies: 1
    Last Post: 03-16-2006, 03:00 PM
  7. Copy Worksheet to specific row in another workbook
    By GregR in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-28-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1