[vba] How to import sheets regardless the name (code inside need a tiny bit of help)

Vexorg

Board Regular
Joined
Oct 5, 2010
Messages
116
Hello,

I want to adapt my code below for a new function I need. This code imports sheets with the name 'Result1, Result2, Result3, etc.' for ranges 1 to 11.

Code:
Public Sub Import_Core()
Dim x As Integer
Dim strSheet As String
    On Error GoTo Handler:
    For x = 1 To 11
        strSheet = "Result " & x & "!"
        DoCmd.TransferSpreadsheet _
            acImport, _
            acSpreadsheetTypeExcel8, _
            "core", _
            "\\msad\root\NA\NY\LIB\fid\FIDSTP\Centralized Reference Data\Clear Exports\core.xls", _
            True, _
            strSheet
    Next x
MsgBox "Core Complete."
Exit Sub
Handler:
MsgBox Err.Description
End Sub</pre>

I now want to import sheets '3 To ShtCount' , each sheet name will be different.


My issue is that I want this to import from all files in a directory, and each sheet may not always be in each file.

Should I just find each of the sheet names that might occur and hard code them in?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi Vexorg,

If I understand your problem correctly, this should do it.

Code:
Public Sub Import_Core()
Dim x As Integer
Dim strSheet As String
    On Error GoTo Handler:
    Workbooks.Open "\\msad\root\NA\NY\LIB\fid\FIDSTP\Centralized Reference Data\Clear Exports\core.xls"
    For x = 3 To ActiveWorkbook.Worksheets.Count
        strSheet = Worksheets(x).Name
        DoCmd.TransferSpreadsheet _
            acImport, _
            acSpreadsheetTypeExcel8, _
            "core", _
            "core.xls", _
            True, _
            strSheet
    Next x
MsgBox "Core Complete."
Exit Sub
Handler:
MsgBox Err.Description
End Sub

I assumed you already have code to loop through all the workbooks in the folder, but if not feel free to follow up. Also, I figured the entire path was not required of the last argument of TransferSpreadsheet since the workbook would be already open, but since I do not have the code for this procedure I can't be sure. If it can't find the workbook, try putting the entire path back in.

Damon
 
Upvote 0
Hi Damon,

Thanks for the code but there are 2 things:

1. Ive updated the code for my specific file, but is there anyway for me to avoid hard coding in the file names so i can import the entire directory?

2. Also when I tried to run this on the file i hard coded in I am receiving an error that highlights

Code:
strSheet = Worksheets(x).Name

And states 'Compiles Error: Sub of Function not defined'


This is the updated code:

Code:
Public Sub Import_Core()
Dim x As Integer
Dim strSheet As String
    On Error GoTo Handler:
    Workbooks.Open "\\msad\root\NA\NY\LIB\fid\FIDSTP\Confirm Renov\Catch All\FCO DB - all\2-2011.xlsx"
    For x = 3 To ActiveWorkbook.Worksheets.Count
        strSheet = Worksheets(x).Name
        DoCmd.TransferSpreadsheet _
            acImport, _
            acSpreadsheetTypeExcel8, _
            "core", _
            "2-2011.xlsx", _
            True, _
            strSheet
    Next x
MsgBox "Core Complete."
Exit Sub
Handler:
MsgBox Err.Description
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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