using a function inside a sub (VBA using Word and Excel) -


i have question using private function inside private sub (command button).

it doesn't return errors... nor anything. when press command button in word doc, supposed form rows , import data excel worksheet until last row in excel, i'm trying function - find last row in worksheet.

if can @ code , let me know if know why won't work, i'd appreciate it. need have function inside private sub commandbutton_2_click()? thank in advance.

private sub commandbutton2_click() dim tbl table dim row row  set tbl = activedocument.tables(2)  dim objexcel new excel.application dim exwb excel.workbook on error resume next  set exwb = objexcel.workbooks.open("s:\electro-protocol\mot_protocols\" & textbox1 & ".xls") dim lastrow integer  lastrow = getlastrow(objexcel, exwb) activedocument.tables(2).columns.distributewidth  counter = 1 lastrow tbl.rows.add tbl.cell(counter, 1).range.text = exwb.sheets("tabelle1").cells(counter, 1) tbl.cell(counter, 2).range.text = exwb.sheets("tabelle1").cells(counter, 2) tbl.cell(counter, 3).range.text = exwb.sheets("tabelle1").cells(counter, 3) tbl.cell(counter, 4).range.text = exwb.sheets("tabelle1").cells(counter, 4) tbl.cell(counter, 5).range.text = exwb.sheets("tabelle1").cells(counter, 5) tbl.cell(counter, 6).range.text = exwb.sheets("tabelle1").cells(counter, 6) next counter  end sub  private function getlastrow(byval objexcel excel.application, byval exwb excel.workbook) integer dim lastrow integer lastrow = 0  exwb.sheets("tabelle1") if objexcel.worksheetfunction.counta(.cells) <> 0     lastrow = .cells.find(what:="*", _                   after:=.range("a1"), _                   lookat:=xlpart, _                   lookin:=xlformulas, _                   searchorder:=xlbyrows, _                   searchdirection:=xlprevious, _                   matchcase:=false).row else     lastrow = 1 end if end end function 

before end function need this:

getlastrow = lastrow 

else function not return value.


Comments