How to take data from the a unopen workbook first sheet without specifying the actual name (because it change every week)

Gwhaou

Board Regular
Joined
May 10, 2022
Messages
78
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello,

For a project I actually use a macro to export data from one unopen workbook sheet to the actual xlsm workbook sheet.
I recently found a problem on the export sheet, this sheet has to be updated every single week but probel the change the name of the sheet changes every week.
But on my export code I need to specify the actual sheet name, my question is : Is there any way to export data from the selected workbook without specifying the name of the sheet (generally is the first sheet)

This is the code :

VBA Code:
Public Sub Export()


Dim Export_Wrk As Workbook
Dim Main_Wrk  As Workbook

Dim WaySheet As String

    With Application.FileDialog(msoFileDialogFilePicker)
   
        If .Show <> 0 Then
       
            WaySheet = .SelectedItems(1)
               
            Set Export_Wrk = Workbooks.Open(WaySheet)
            Set Main_Wrk = Workbooks("Template.xlsm")

            Export_Wrk.Sheets("export_week1").Activate ' I have to specify the name of the sheet
            Last_ligne = Export_Wrk.Sheets("export_week1").Cells(Rows.Count, "A").End(xlUp).Row ' I have to specify the name of the sheet
            Main_Wrk.Sheets("export_week1").Range("A1:AG" & Last_ligne).Copy ' I have to specify the name of the sheet

   
            Main_Wrk Sheets("Export_1").Activate
            Range("A1").Select
            ActiveSheet.Paste
   

            Application.CutCopyMode = False
            Export_Wrk.Close
       
        Else
            MsgBox "You have canceled the transfert"
            Exit Sub
       
        End If
        End With
    
End Sub

I will appreciate some help 🙏
Remark : The unopen workbook is a csv file which is weekly uploaded on the database so The name of the workbook and the name of the first sheet is the same.
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
As the file is a csv file it will only have one sheet, so you can use
VBA Code:
Export_Wrk.Sheets(1)
 
Upvote 0
Solution
As the file is a csv file it will only have one sheet, so you can use
VBA Code:
Export_Wrk.Sheets(1)
yes sir, it works perfectly.

I have a doubt, let's take a example of a xlsx file with more than one sheet and I want to copy data only from the first sheet or the third.
In this case
VBA Code:
 Export_Wrk.Sheets(1)
or
VBA Code:
 Export_Wrk.Sheets(3)
will work ?
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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