change code from open file to close file when bring value from open file

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
144
Office Version
  1. 2019
Platform
  1. Windows
Hello
I use this code to sum the whole column(G) from DATA file and populate value in A1 for MASTER file .
I have to make both files are open . How I make DATA file is closed after brings value without the file has already open?



VBA Code:
Sub test()
Dim wb1, wb2 As Workbook
Set wb1 = Workbooks("MASTER")
Set wb2 = Workbooks("DATA")
wb1.Sheets("Sheet1").Cells(1, 1).Value = Application.Sum(wb2.Sheets("MAIN").Columns(7))
End Sub
thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Change the path and file extension of the DATA workbook as required.

VBA Code:
Sub test()
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = Workbooks("MASTER")
    Set wb2 = Workbooks.Open("C:\path\to\DATA.xlsx")
    wb1.Sheets("Sheet1").Cells(1, 1).Value = Application.Sum(wb2.Sheets("MAIN").Columns(7))
    wb2.Close False
End Sub
 
Upvote 0
Solution
great !
this is what I look for it.(y)
thank you so much, John.:)
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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