excel vba - Why does code fail on second run but not first? -
this code working on first run, when try run again following error:
run-time error '91': object variable or block variable not set...
private sub cmdingresar_click() dim excelapp excel.application dim excelwb excel.workbook dim excelws excel.worksheet set excelapp = createobject("excel.application") set excelwb = excelapp.workbooks.open("c:\users\vere\desktop\carrera desarrollo\visual 6\sistema mascara mahe\cheques.xlsx") excelapp.visible = false = txtcheque.text b = txtfechadeelaboracion.text c = txtfechadecobro.text e = txtfactura.text f = txtproveedor.text h = txtnumerodecuenta.text = txtrfc.text l = txtdescripcion.text n = txtneto.text o = txtprecio.text selection.entirerow.insert ' here appers error when try second run activesheet.cells(3, 1).select activesheet.cells(1 + 3, 1) = activesheet.cells(1 + 3, 2) = b activesheet.cells(1 + 3, 3) = c activesheet.cells(1 + 3, 4) = d activesheet.cells(1 + 3, 5) = e activesheet.cells(1 + 3, 7) = f activesheet.cells(1 + 3, 8) = g activesheet.cells(1 + 3, 9) = h activesheet.cells(1 + 3, 10) = activesheet.cells(1 + 3, 11) = j activesheet.cells(1 + 3, 12) = k activesheet.cells(1 + 3, 13) = l activesheet.cells(1 + 3, 14) = m activesheet.cells(1 + 3, 15) = n activesheet.cells(1 + 3, 16) = o excelwb.saved = true set excelws = nothing set excelwb = nothing excelapp.quit set excelapp = nothing msgbox "listo" end sub
you have lot of unqualified object references there causing problem. think want:
private sub cmdingresar_click() dim excelapp excel.application dim excelwb excel.workbook dim excelws excel.worksheet set excelapp = createobject("excel.application") set excelwb = excelapp.workbooks.open("c:\users\vere\desktop\carrera desarrollo\visual 6\sistema mascara mahe\cheques.xlsx") excelapp.visible = false = txtcheque.text b = txtfechadeelaboracion.text c = txtfechadecobro.text e = txtfactura.text f = txtproveedor.text h = txtnumerodecuenta.text = txtrfc.text l = txtdescripcion.text n = txtneto.text o = txtprecio.text set excelws = excelwb.activesheet excelws .cells(3, 1).entirerow.insert .cells(1 + 3, 1).value = .cells(1 + 3, 2).value = b .cells(1 + 3, 3).value = c .cells(1 + 3, 4).value = d .cells(1 + 3, 5).value = e .cells(1 + 3, 7).value = f .cells(1 + 3, 8).value = g .cells(1 + 3, 9).value = h .cells(1 + 3, 10).value = .cells(1 + 3, 11).value = j .cells(1 + 3, 12).value = k .cells(1 + 3, 13).value = l .cells(1 + 3, 14).value = m .cells(1 + 3, 15).value = n .cells(1 + 3, 16).value = o end ' if want save file, won't work! excelwb.saved = true ' need use excelwb.close savechanges:=true set excelws = nothing set excelwb = nothing excelapp.quit set excelapp = nothing msgbox "listo" end sub
Comments
Post a Comment