Open, Copy data and Close a workbook based on the names of the tabs in my active workbook

Aurore87

New Member
Joined
Nov 26, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,
Apologies English is not my first language and I am very new to writing VBA codes. So far, I have only copied and adapted parts of the codes to fit my needs.

Recently, I have created a workbook, each tab has the name of a treatment site. Daily, I receive data about these treatment sites in separate workbooks.
Active workbook = Workbook that I have created
Daily workbooks = Workbooks I receive daily
Folder path = Active workbook, Dashboard tab, Cell b3
What I want my macro to do is: -
1- for each tab of my "active workbook", find in my folder the "daily workbook" that has the same name as the tab in my "active workbook", then
2- open this "daily workbook", copy the selection (the data is already selected), paste it to the corresponding tab (same name) in cell b6 of my "active workbook", close the "daily workbook" (no need to save) and repeat for the following tab of my "active workbook".

I am sure this is very easy but I am struggling a lot with this, especially the first part of the question.
Could anybody help me, please?



Many thanks
Aurore
 

Attachments

  • Screenshot 2 .jpg
    Screenshot 2 .jpg
    176.9 KB · Views: 5

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this. Assumes the daily workbooks are .xlsx files.

VBA Code:
Public Sub Copy_Selection_From_Tab_Workbooks()

    Dim sourceFolder As String
    Dim ws As Worksheet
    Dim sourceWorkbook As Workbook

    Application.ScreenUpdating = False
    
    With ActiveWorkbook
        sourceFolder = .Worksheets("Dashboard").Range("B3").Value
        If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
        For Each ws In .Worksheets
            If StrComp(ws.Name, "Dashboard", vbTextCompare) <> 0 Then
                Set sourceWorkbook = Workbooks.Open(sourceFolder & ws.Name & ".xlsx")
                Selection.Copy ws.Range("B6")
                sourceWorkbook.Close False
            End If
        Next
    End With
    
    Application.ScreenUpdating = True
    
    MsgBox "Done"
    
End Sub
 
Upvote 0
Hi @John_w,

Sorry for the late reply, I was on site visits and so away from my laptop.

I just tried it out and it worked "HURRAY"!

Thank you so much for your help!
A.
 
Upvote 0

Forum statistics

Threads
1,224,272
Messages
6,177,632
Members
452,786
Latest member
k3calloway

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