vba - Loop Through Userform & Paste to Offset Cells -
it's me again!
i trying input data database userform looping through each control , pasting cell via offset counter. getting error on line inputs data cell , cannot figure out how via loop. easy field field not want write many lines of code.
here recent attempt:
option explicit sub cbsubmit_click() ' set worksheet dim dbfood worksheet set dbfood = sheets("dbfood") 'set last row , column dim lrow long lrow = cells(rows.count, 1).end(xlup).row dim lcol long lcol = cells(1, columns.count).end(xlleft).row 'define idcell range type dim idcell range ' if no records exit, add first record if cells(lrow, 1).value = "id" set idcell = dbfood.range("a2") idcell.value = 1 ' add data dim ufcontrol control dim counter long counter = 1 each ufcontrol in me.controls if typeof ufcontrol msforms.combobox or msforms.textbox idcell.offset(0, counter).value = uffield.value counter = counter + 1 end if next ufcontrol msgbox "added database!" ' else add next record elseif cells(lrow, 1).value >= 0.1 dim lastid long lastid = cells(lrow, 1).value set idcell = dbfood.cells(lrow + 1, 1) idcell.value = lastid + 1 ' add data ' if none of above display error , exit sub else: msgbox ("error - cannot create record") exit sub end if end sub
if me figure out how solve 1 great!
i saw things have adapted down below. may ask test bit of code?
option explicit sub cbsubmit_click() dim dbfood worksheet set dbfood = activeworkbook.sheets("dbfood") dim lrow long lrow = dbfood.cells(dbfood.rows.count, 1).end(xlup).row dim lcol long lcol = dbfood.cells(1, dbfood.columns.count).end(xlleft).row dim idcell range if dbfood.cells(lrow, 1).value = "id" set idcell = dbfood.range("a2") idcell.value = 1 dim ufcontrol control dim counter long counter = 1 each ufcontrol in me.controls if typeof ufcontrol msforms.textbox idcell.offset(0, counter).value = ufcontrol.result counter = counter + 1 elseif typeof ufcontrol msforms.combobox idcell.offset(0, counter).value = ufcontrol.seleteditem.value end if next ufcontrol msgbox "added database!" elseif dbfood.cells(lrow, 1).value >= 0.1 dim lastid long lastid = dbfood.cells(lrow, 1).value set idcell = dbfood.cells(lrow + 1, 1) idcell.value = lastid + 1 else msgbox ("error - cannot create record") exit sub end if end sub
as can see have divided types of ufcontrol not sure combobox can directly .value you'll have add .selecteditem. can @ least try once :)
Comments
Post a Comment