Need help - how to copy between workbook until the last row, with the name of the source is starting with "AXREP*" in Excel VBA

Cakz Primz

Board Regular
Joined
Dec 4, 2016
Messages
102
Office Version
  1. 365
Platform
  1. Windows
Dear all,

I have to open a latest update file in share folder, the name of the workbook is always changing, but it always begin with this word "AXREP_20221103.xlsb", or AXREP_20221104.xlsb", etc.
And it only has one sheet "AXREP", this will become my source.

I have the code below, after the workbook is being opened:
VBA Code:
Sub CopyAXREP()
    Dim wb As Workbook

    For Each wb In Application.Workbooks
    If wb.Name Like "AXREP*" Then wb.Activate: Exit Sub '===Sub is exited at this point, so if the workbook is open, no further code will be executed. So the result would simply be the Actuals Repository workbook would be activated
    Next wb
    Sheets("AXREP").Activate
    
    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Dim lRow As Long
    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    With ws
        .Range("B2:B" & lRow).Copy Workbooks("Target").Sheet("Dataset").Range("B2")
    End With
    Workbooks("AXREP*.xlsb").Close SaveChanges:=True
End Sub

But it stop in this code:
Code:
    Sheets("AXREP").Activate

How I can active "AXREP" sheet in source workbook ("AXREP*.xlsb) so I can copy from column B until the last row?

Thanks in advance.
Prima Indonesia
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I
Dear all,

I have to open a latest update file in share folder, the name of the workbook is always changing, but it always begin with this word "AXREP_20221103.xlsb", or AXREP_20221104.xlsb", etc.
And it only has one sheet "AXREP", this will become my source.

I have the code below, after the workbook is being opened:
VBA Code:
Sub CopyAXREP()
    Dim wb As Workbook

    For Each wb In Application.Workbooks
    If wb.Name Like "AXREP*" Then wb.Activate: Exit Sub '===Sub is exited at this point, so if the workbook is open, no further code will be executed. So the result would simply be the Actuals Repository workbook would be activated
    Next wb
    Sheets("AXREP").Activate
   
   
    Dim ws As Worksheet
    Set ws = ActiveSheet
   
    Dim lRow As Long
    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    With ws
        .Range("B2:B" & lRow).Copy Workbooks("Target").Sheet("Dataset").Range("B2")
    End With
    Workbooks("AXREP*.xlsb").Close SaveChanges:=True
End Sub

But it stop in this code:
Code:
    Sheets("AXREP").Activate

How I can active "AXREP" sheet in source workbook ("AXREP*.xlsb) so I can copy from column B until the last row?

Thanks in advance.
Prima Indonesia

For your additional information, how to activate source workbook, with the name starting with "AXREP_":
VBA Code:
Windows("AXREP_*.xlsb").Activate

Thank you very much
Prima Indonesia
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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