
Originally Posted by
fixer
...
And one more thing Sir vba is placed in a seperate file means it is not placed in 9.xlsb and 1.xls it will be placed in a different file and all files are located in a same place (9.xlsb,1.xls and vba placed file all are located in same place)
only one file is opened that is vba code placed file
OK. I will use file Macro.xlsm for the Macro, Sub Vixer4()
Code:
Sub Vixer4() ' http://www.excelfox.com/forum/showthread.php/2353-add-by-vba?p=11425&viewfull=1#post11425
Rem 1 Workbook and worksheets info
'1a) Workbook info
Dim Wb1 As Workbook, Wbm As Workbook ' (These will be set later when the workbooks are opened)
Dim strWb1 As String: Let strWb1 = "1.xls"
Dim strWbm As String: Let strWbm = "9.xlsb"
'1b) Worksheets info
Dim Ws1 As Worksheet, Wsm As Worksheet ' (These will be set later when the workbooks are opened)
Dim Lr As Long: Let Lr = 121 ' For this example I am using 120 rows of data ( 2 - 121 )
Rem 2 Open 1.xls file
'Workbooks.Open Filename:= "F:\Excel0202015Jan2016\ExcelFox\vixer\Von Vixer\1.xls"
'Workbooks.Open Filename:=ThisWorkbook.Path & "\" & "1.xls" ' all files are located in a same place ' 9.xlsb and 1.xls are in the same folder, ( so I can use ThisWorkbook.Path to get the string path to the folder )
Workbooks.Open Filename:=ThisWorkbook.Path & "\" & strWb1
Set Wb1 = ActiveWorkbook ' The workbook just opened will now be the current active workbook
Set Ws1 = Wb1.Worksheets.Item(1)
Workbooks.Open Filename:=ThisWorkbook.Path & "\" & strWbm
Set Wbm = ActiveWorkbook '
Set Wsm = Wbm.Worksheets.Item(1)
Rem 3 add all the data in column H of 1.xls and paste the result in K9 of vba contained file in sheet1
'Wbm.Activate: Wsm.Range("K9").Select: Application.Calculation = xlCalculationAutomatic
' Let Wsm.Range("K9").Value = "=SUM('[1.xls]1-Sheet1'!$H$2:$H$121)" ' Note: In VBA we must write the formulas in English
' Let Wsm.Range("K9").Value = "=SUM('[" & strWb1 & "]1-Sheet1'!$H$2:$H$121)"
' Let Wsm.Range("K9").Value = "=SUM('[" & strWb1 & "]" & Ws1.Name & "'!$H$2:$H$121)"
Let Wsm.Range("K9").Value = "=SUM('[" & strWb1 & "]" & Ws1.Name & "'!$H$2:$H$" & Lr & ")"
Let Wsm.Range("K9").Value = Wsm.Range("K9").Value ' change cell content from formula to its value
Rem 4 close the files
Wbm.Close savechanges:=True
Wb1.Close
End Sub
Alan
Bookmarks