Excel VBA: Can't create sheets on called another Excel file -
the goal open excel file parameters first / main file , call macro creates 1 or n new sheets data database, excel won't create new sheets in second file , other logic fails.
you can find sample code 2 files below. when b file opened manually , called tst() sub, works, not when first file opens second file. workbooks not protected, i'm using ms excel 2010.
a_file.xlsm main file user calls getfile open file , run readparams macro. code located in modules.
sub getfile(filename string) dim filepath, par1, par2, currentuser string dim targetfile workbook currentuser = createobject("wscript.network").username filepath = "c:\users\" & currentuser & "\documents\excel_apps\" par1 = "use_r_one" par2 = "some_val" application.screenupdating = false set targetfile = workbooks.open(filepath & "b_file.xlsm") application.run "'" & targetfile.name & "'!readparams(" & chr(34) & par1 & chr(34) & ", " & chr(34) & par2 & chr(34) & ")" targetfile.activate application.screenupdating = true end sub
b_file.xlsm macros:
sub readparams(s_uno string, s_duo string) if isnull(s_uno) or isnull(s_duo) msgbox "error occurred.", vbexclamation, "error" else msgbox "all params ok, new sheet coming right after msg" thisworkbook.worksheets.add(after:=worksheets(worksheets.count)).name = "sheet_data" '<-- won't work end if end sub sub tst() readparams "use_r", "test" end sub
the problem in line application.run...
syntax
application.run "'b.xlsm'!readparams", par1, par2
you had parameters concatenated first argument
Comments
Post a Comment