excel vba - How to add activeX buttons programmatically in VBA, filling all rows down a column -
my first post here, have been sourcing solutions , ideas website while now. collection of solutions , ideas.
basically, have spread sheet application requiring first column, column a, filled "active x" buttons in every cell, looping through given quantity. have posted 1 such working solution below makes use of "form type buttons" , modules. exemplifies consider favored example working buttons. once operational column of buttons correspond relative data on same row, , when clicked open corresponding folders, , userforms in later developments.
the second post uses range function, doesn't incorporate buttons interactive with. however, mouse click on range activate code within worksheet_selection change procedure...sorry stating obvious!
what have been trying achieve version of code employing "activex" command buttons, after having studied great tutorials , poured on range of programing concepts, still fail miserably employ oleobjects.
how add button programmatically in vba next sheet cell data?
sheet 1 procedure: sub columna_buttons()
dim buttons button dim rng range dim lineqty variant application.screenupdating = false activesheet.buttons.delete lineqty = 5 = 1 lineqty set rng = activesheet.range(cells(i, 1), cells(i, 1)) set buttons = activesheet.buttons.add(rng.left, rng.top, rng.width, rng.height) buttons .onaction = "buttons" .caption = "line " & .name = "line " & end next application.screenupdating = true end sub public click_button variant ' make variable public userform1
'
form button module:
sub line_buttons() click_button = application.caller msgbox click_button & " clicked" userform1.show 'launch custom userform end sub
and next option considered range detection
private sub worksheet_selectionchange(byval target range) ' e.g., range(a1:e1) clicked if not application.intersect(target, range("b2:b12")) nothing msgbox "you clicked " & target.address end if end sub
here example of activex buttons being created , coded run. may take small tweaks, job done.
sub createbutton() dim obj object dim code string dim cellleft single dim celltop single dim cellwidth single dim cellheight single dim lineqty integer sheets("sheet1").select lineqty = 5 = 1 lineqty set rng = activesheet.range(cells(i, 1), cells(i, 1)) cellleft = rng.left celltop = rng.top cellwidth = rng.width cellheight = rng.height 'create button set obj = activesheet.oleobjects.add(classtype:="forms.commandbutton.1", link:=false, displayasicon:=false, left:=cellleft, top:=celltop, width:=cellwidth, height:=cellheight) obj.name = "testbutton" 'button text activesheet.oleobjects(1).object.caption = "test button" 'macro text added possibly array? code = "private sub testbutton_click()" & vbcrlf code = code & "call tester" & vbcrlf code = code & "end sub" 'add macro @ end of sheet module activeworkbook.vbproject.vbcomponents(activesheet.name).codemodule .insertlines .countoflines + 1, code end next end sub sub tester() msgbox "you have clicked on test button" end sub
note in order not error on me, had go trust center , trust center settings , macro settings , check box "trust access vba project object model"
Comments
Post a Comment