Copying external sheet to current workbook, Excel VBA

Meesam

Board Regular
Joined
Nov 23, 2011
Messages
66
I'm working on small project in which I need to add sheets to the currently open workbook from any external database of worksheets in another workbook. I made a form to import any sheet that is required in the currently open (active) workbook. The sheets will be copied from remote (in any other folder but same computer) workbook. I am using following code but due to unknown reasons the sheets are NOT getting copied to my current workbook. Urgent help is appreciated.


<tbody>
</tbody>

Code:
Dim wb As Workbook
Dim activeWB As Workbook
Dim FilePath As String
Dim oWS      As String
Set activeWB = Application.ActiveWorkbook

FilePath = "D:\General Required Docs\DATA.xlsm"

If optFirst.Value = True Then
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   On Error Resume Next
   oWS = cboMaterial.Value
   Set wb = Application.Workbooks.Open(FilePath)
   wb.Worksheets(oWS).Copy      
   After:=Application.ActiveWorkbook.Sheets(ThisWorkbook.Sheets.count)
   activeWB.Activate
   wb.Close False

   Application.ScreenUpdating = True
   Application.DisplayAlerts = True
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Is the On Error Resume Next there for a specific reason? Does the code raise an error if you remove it?

If you enable DisplayAlerts, are you getting all the alerts you're expecting?

(Generally, develop code with messages and alerts enabled until you're sure you can safely ignore them. Same goes for erroring.)

Have you tried stepping through the code to see if each statement is doing what you expect it to?
 
Upvote 0
Ah no, I think I got it:-
Code:
wb.Worksheets(oWS).Copy After:=[COLOR=#ff0000][B]This[/B][/COLOR]Workbook.Sheets(ThisWorkbook.Sheets.count)

Single-stepping the code reveals this nicely.
 
Upvote 0
Ah no, I think I got it:-
Code:
wb.Worksheets(oWS).Copy After:=[COLOR=#ff0000][B]This[/B][/COLOR]Workbook.Sheets(ThisWorkbook.Sheets.count)

Single-stepping the code reveals this nicely.

Actually I tried this method twice but to no avail. I just got reply from another expert and it really worked like a charm.
The Correct solution is:

Code:
After:=<code>[COLOR=#ff0000][B]activeWB[/B][/COLOR].Sheets([COLOR=#ff0000][B]activeWB[/B][/COLOR].Sheets.count)</code>

I removed display alerts as they are useless and cause unnecessary interruption. Also I used On Error if there is some sheet missing in the original database then I don't want scary pop-up box or window.

Anyways thanks for your help
 
Upvote 0

Forum statistics

Threads
1,215,639
Messages
6,125,968
Members
449,276
Latest member
surendra75

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