VBA Copy ONLY worksheets of the same tab name

davehnj

Board Regular
Joined
Jul 2, 2010
Messages
56
I have a workbook with several tabs. I need to copy from another workbook ONLY the tabs with the same tab-name. I need this to be done in VBA. Any help would be appreciated. I am stumpted after many many tries at this.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I have the code for getting the second Excel file (the one I'm copying from) and I have two loops (one within the other) - one that presumably will cycle through the source file sheets and one that will cycle through the destination file sheets and determine if the sheet names are the same. if they are, I attemp to copy the source contents to the destination contents.

it is failing because I cannot seem to ascertain which sheet is active at any given time

This is my code to-date:

Sub Import_source_sheets()

Dim sFile As String
Dim lX As Long
Dim x As Integer
Dim wks As Long
Dim wkd As Long


sFile = Application.GetOpenFilename(FileFilter:="Excel files, *.*")
If sFile = "False" Then GoTo End_Sub
Workbooks.Open Filename:=sFile
Set wkbdest = ActiveWorkbook
Set wkbsource = Workbooks.Open(sFile)
For wks = 1 To wkbsource.Worksheets.Count
For wkd = 1 To ActiveWorkbook.Worksheets.Count
If wkbsource.Worksheets(wks).Name = ActiveWorkbook.Worksheets(wkd).Name Then
wkbsource.Worksheets(wks).Select
ActiveSheet.UsedRange.Copy Destination:=ThisWorkbook.Worksheets(wkd).Range("A1")
End If
Exit For
Next wkd
Exit For
Next wks

ActiveWorkbook.Saved = True
ActiveWindow.Close

End_Sub:

End Sub
 
Upvote 0
Why are you opening the same workbook twice?

Code:
Workbooks.Open Filename:=sFile
Set wkbdest = ActiveWorkbook
Set wkbsource = Workbooks.Open(sFile)

When you open a workbook it becomes the ActiveWorkbook. The workbook that contains your code is ThisWorkbook. You don't need to activate/select to copy:

Code:
wkbsource.Worksheets(wks).UsedRange.Copy Destination:=ThisWorkbook.Worksheets(wkd).Range("A1")
 
Upvote 0
Try (untested):

Code:
Sub Import_source_sheets()
    Dim sFile As Variant
    Dim wkbDest As Workbook, wkbSource As Workbook
    Dim wks As Long
    Dim wkd As Long
    sFile = Application.GetOpenFilename(FileFilter:="Excel files, *.*")
    If sFile = False Then Exit Sub
    Set wkbDest = ThisWorkbook
    Set wkbSource = Workbooks.Open(sFile)
    For wks = 1 To wkbSource.Worksheets.Count
        For wkd = 1 To wkbDest.Worksheets.Count
            If wkbDest.Worksheets(wkd).Name = wkbSource.Worksheets(wks).Name Then
                wkbSource.Worksheets(wks).UsedRange.Copy Destination:=wkbDest.Worksheets(wkd).Range("A1")
                Exit For
            End If
        Next wkd
    Next wks
    wkbSource.Close SaveChanges:=False
End Sub
 
Upvote 0
Thank you so much, Andrew. This helped a lot. I am still cancelling out since my source sheets have meged cells and I am getting a message that says :Merged cells cannot be changed or deleted". How can I get around this?
 
Upvote 0
Yes, I realize that. Unfortunately, the source file is being supplied by a client (over whose actions I have no control) :mad:
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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