vba - Run-time error '1004': Method 'Range' of object '_worksheet' failed while merging -
option explicit public sub compile_workbook_data() dim master_wkbk workbook: set master_wkbk = thisworkbook dim master_sht worksheet: set master_sht = thisworkbook.worksheets("task tracking-internal & org.") dim current_wkbk workbook dim current_sht worksheet dim wkbk_list(1 3) string dim x integer dim last_row integer dim last_col integer wkbk_list(1) = "sub project_workbook - core services.xlsm" wkbk_list(2) = "sub project_workbook - esp2.0.xlsm" wkbk_list(3) = "sub project_workbook - p2e.xlsm" x = 1 ubound(wkbk_list) set current_wkbk = workbooks.open("d:\delta dental\" & wkbk_list(x)) set current_sht = current_wkbk.worksheets("task tracking-internal & org.") last_row = current_sht.cells.find("*", searchorder:=xlbyrows, searchdirection:=xlprevious).row last_col = current_sht.cells.find("*", searchorder:=xlbycolumns, searchdirection:=xlprevious).column current_sht.range(cells(4, 1), cells(last_row, last_col)).copy last_row = master_sht.cells.find("*", searchorder:=xlbyrows, searchdirection:=xlprevious).row master_sht.range("a" & last_row + 1).pastespecial paste:=xlpastevalues current_wkbk.close false next x end sub
im getting following error while running merge code:
run-time error '1004': method 'range' of object '_worksheet' failed
the following code code. yet, made bit more verbose. might allow tell error is:
option explicit public sub compile_workbook_data() dim master_wkbk workbook dim master_sht worksheet dim current_wkbk workbook dim current_sht worksheet dim wkbk_list(1 3) string dim x integer dim last_row integer dim last_col integer dim bolfound boolean dim strfilepath string dim strsheetname string dim fso new filesystemobject set master_wkbk = thisworkbook strsheetname = "task tracking-internal & org." strfilepath = "e:\delta dental\" bolfound = false each master_sht in master_wkbk.worksheets if master_sht.name = strsheetname bolfound = true: exit next master_sht if bolfound = false msgbox "couldn't find required sheet in excel file." & chr(10) & "aborting...": exit sub wkbk_list(1) = "sub project_workbook - core services.xlsm" wkbk_list(2) = "sub project_workbook - esp2.0.xlsm" wkbk_list(3) = "sub project_workbook - p2e.xlsm" if not fso.driveexists(split(strfilepath, "\")(0)) msgbox "couldn't find required drive" & split(strfilepath, "\")(0) & "." & chr(10) & "aborting...": exit sub if not fso.folderexists(strfilepath) msgbox "couldn't find required folder." & chr(10) & "aborting...": exit sub x = 1 ubound(wkbk_list) if dir(strfilepath & wkbk_list(x)) = vbnullstring msgbox "file " & chr(10) & " " & strfilepath & wkbk_list(x) & chr(10) & "not found." & chr(10) & "aborting...": exit sub set current_wkbk = workbooks.open("d:\delta dental\" & wkbk_list(x)) bolfound = false each current_sht in current_wkbk.worksheets if current_sht.name = strsheetname bolfound = true: exit next current_sht if bolfound = false msgbox "couldn't find required sheet in file:" & chr(10) & " " & strfilepath & wkbk_list(x) & chr(10) & "aborting...": exit sub last_row = current_sht.cells.find("*", searchorder:=xlbyrows, searchdirection:=xlprevious).row last_col = current_sht.cells.find("*", searchorder:=xlbycolumns, searchdirection:=xlprevious).column current_sht.range(cells(4, 1), cells(last_row, last_col)).copy last_row = master_sht.cells.find("*", searchorder:=xlbyrows, searchdirection:=xlprevious).row master_sht.range("a" & last_row + 1).pastespecial paste:=xlpastevalues current_wkbk.close false next x end sub
note, above code requires reference microsoft scripting runtime. please make sure set before running code.
Comments
Post a Comment