excel - access sub runs once then Error '91': object variable not set -
thank in advance,
i have access database contains following code. setup open defined excel file, locate last row , add 1 value of row in next row below, save etc... first time, if run again "error '91': object variable or block variable not set". however, if hit stop button (reset) in vba window or end button on error message, run again works. i've tried many things , i'm @ loss.
any suggestions?
private sub quotenew1_click() dim app new excel.application app.visible = true dim book excel.workbook set book = app.workbooks.add("c:\desktop\test_quotes.xlsx") nextemptyrow 'find last row , add new value next row colum book.close savechanges:=true app.quit set app = nothing end sub sub nextemptyrow() dim emptyrow long emptyrow = activesheet.usedrange.rows.count msgbox emptyrow cells(emptyrow + 1, 1).value = cells(emptyrow, 1).value + 1 end sub
kind regards,
workbooks.add wants template argument. want use format below adding new workbook first time. after that, want open existing workbook. following code works me , appears meet requirement of adding new row each time incremented value.
private sub quotenew1_click() dim app new excel.application app.visible = true dim book excel.workbook 'if workbook exists open it, otherwise create if len(dir("c:\temparea\test_quotes.xlsx")) = 0 'file doesn't exist, create set book = workbooks.add app.displayalerts = false book.saveas filename:="c:\temparea\test_quotes.xlsx" app.displayalerts = false else 'open existing file set book = workbooks.open("c:\temparea\test_quotes.xlsx") end if nextemptyrow 'find last row , add new value next row colum book.close savechanges:=true app.quit set app = nothing end sub sub nextemptyrow() dim emptyrow long emptyrow = activesheet.usedrange.rows.count if cells(emptyrow, 1).value = 0 activesheet.usedrange.value = activesheet.usedrange.value + 1 else cells(emptyrow + 1, 1).value = cells(emptyrow, 1).value + 1 end if end sub
Comments
Post a Comment