Importing Worksheet with same name always goes to first one imported

mentalp

New Member
Joined
Jul 21, 2015
Messages
1
Hi

I am new to VBA and trying to import worksheets from other workbooks. I have tried looking at forums to get an answer but can't find my example. I am using Excel 2010 (in a Cirtix environment - if that makes a difference).

I open a dialogue box to get the name of the workbook and it imports the first worksheet with no problems. The workbooks are differnt but the sheet I want o import is always the same name ("Detailed Overall") so the code is designed to change the name of the first sheet.

The problem is when I try to import another worksheet, it just imports the first one again. I have deleted the sheets and tried importing the second in the first instance but it still imports the first worksheet.

I assume it is an issue with the way I am naming/calling my workbooks.

Code:
Sub OpenWorkBook()</SPAN>
 
    Dim ws As Worksheet</SPAN>
    Dim wsData As Boolean</SPAN>
   
    wsData = False</SPAN>
   
    For Each ws In ThisWorkbook.Sheets</SPAN>
   
       If ws.Name = "Detailed Overall" Then</SPAN>
            wsData = True</SPAN>
        End If</SPAN>
       
    Next ws</SPAN>
   
    If wsData = True Then</SPAN>
        wsName = " Original for " & Sheets("Detailed Overall").Range("B10").Value</SPAN>
       
            Sheets("Detailed Overall").Name = wsName</SPAN>
   
    End If</SPAN>
   
    myFilePath = Application.GetOpenFilename()</SPAN>
   
    'if user cancels</SPAN>
   
    If myFilePath = False Then</SPAN>
        Target = ""</SPAN>
   
    Else</SPAN>
        Target = myFilePath</SPAN>
   
    End If</SPAN>
   
    Workbooks.Open Filename:=myFilePath, UpdateLinks:=3</SPAN>
    wbName = ActiveWorkbook.Name</SPAN>
   
   
    Sheets("Detailed Overall").Select</SPAN>
    Sheets("Detailed Overall").Copy After:=ThisWorkbook.Sheets(1)</SPAN>
    Windows(wbName).Activate</SPAN>
    ActiveWindow.Close</SPAN>
   
Me.Select</SPAN>
   
   
End Sub
</SPAN>
Any assisatnce is greatly appreciated.

Thanks
Ewan
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi and welcome to the MrExcel Message Board.

My compiler did not like the "Me.Select" command so I removed it.

Apart from that everything seemed to work. So I don't know what to say.

I created a workbook with the macro in it which just had a worksheet called Sheet1.
Then I created four more called DO1-4. They all had a number between 1 and 4 in A1 and the filename (e.g. DO1) in B10.
When I ran the macro I ended up with a workbook with Sheet1 and Detailed Overall worksheets.
Running it again resulted in Sheet1, Detailed Overall and Original for DO1. The Detailed Overall worksheet had the figure "2" in it indicating it was the second workbook.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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