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

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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