import only sheet in file regardless of name

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
Hey Guys

I have the below. I just want it to open the file and then import the sheet regardless of the file name. Basically it has one sheet and they client keeps changing the name and they arent going to be consistent so I need a solution. Any ideas? The sheet is called IPAC PROCESSED in this example.

Jordan

VBA Code:
Sub DCASIMPORTIPACPROCESSED()
Worksheets.Add().Name = "IPAC PROCESSED"
Dim myfile As String
Dim erow As Long
Dim filepath As String
Dim wb1 As Workbook, wb2 As Workbook

    Dim fn As String

fn = Left(ThisWorkbook.Worksheets("Variables").Range("A1").Value, 6)
    
Application.ScreenUpdating = False
Set wb1 = ThisWorkbook
filepath = "K:\SHARED\TRANSFER\Enterprise Wide Suspense Initiative\Source Files\DCAS\" & ThisWorkbook.Worksheets("Variables").Range("A4").Value & "\" & fn & Right(ThisWorkbook.Worksheets("Variables").Range("a2").Value, 2) & "\"
myfile = Dir(filepath)
Do While Len(myfile) > 0 And myfile <> "suspense automation.xlsm"

    erow = wb1.Sheets("IPAC PROCESSED").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Set wb2 = Workbooks.Open(filepath & myfile)
    With wb2
        .Sheets("IPAC Processed").Range("A2:n300").Copy Destination:=wb1.Worksheets("IPAC PROCESSED").Cells(erow, 1)
        .Close savechanges:=False
    End With
    myfile = Dir
Loop
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about
VBA Code:
    With wb2
        .Sheets(1).Range("A2:n300").Copy Destination:=wb1.Worksheets("IPAC PROCESSED").Cells(erow, 1)
        .Close savechanges:=False
    End With
 
Upvote 0
How about
VBA Code:
    With wb2
        .Sheets(1).Range("A2:n300").Copy Destination:=wb1.Worksheets("IPAC PROCESSED").Cells(erow, 1)
        .Close savechanges:=False
    End With
worked sweet! I assume it just imports the first sheet right? so if there is a second sheet I could just move the 1 to a 2?

THANKS FLUFF!
 
Upvote 0
I think the best way is to count the number of sheets in the workbook. if Count = 1 then rename that sheet to " IPAC PROCESSED" . In that way you will always have one consistent name

count = wb2.sheets.count

then maybe,

For each ws in wb2.sheets
ws.name = "IPAC PROCESSED "
next ws
 
Upvote 0
I think the best way is to count the number of sheets in the workbook. if Count = 1 then rename that sheet to " IPAC PROCESSED" . In that way you will always have one consistent name

count = wb2.sheets.count

then maybe,

For each ws in wb2.sheets
ws.name = "IPAC PROCESSED "
next ws
ok thanks bud!
 
Upvote 0
That assumes the OP wants it saved. ;)
If there is only one sheet in the workbook, the name is irrelevant. If there is more than one sheet, then there are better way of ensuring the correct sheet is used.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

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