excel vba - Copying cell with VBA using If statement -
i'm beginner vba , i'm wondering how add if else statement code: want enable copy cells if filled , if not filled msgbox must pop-up
code:
private sub commandbutton3_click() application.screenupdating = false dim nextrow range sheet1.range("f7,f10,f13,f16,f19,f22,f25,f28").copy sheets("overzicht").select set nextrow = activesheet.cells(cells.rows.count, 1).end(xlup).offset(1, 0) nextrow.select selection.pastespecial (xlvalues), transpose:=true msgbox "invoer opgeslagen" application.cutcopymode = false application.screenupdating = true end sub
welcome stackoverflow.com
you have wrap copy code block
for loop
, if-else
statement , boolean
type variable.
firstly, want iterate on specified range of cells , make sure filled
dim allfilled boolean dim long = 7 28 step 3 if not isempty(sheet1.range("f" & i)) allfilled = true else allfilled = false end if next
if can proceed copying-pasting , if not program show message box: not cells filled! cant copy
your complete code:
sub commandbutton3_click() application.screenupdating = false dim allfilled boolean dim long = 7 28 step 3 if not isempty(sheet1.range("f" & i)) allfilled = true else allfilled = false end if next if allfilled ' = if (allfilled = true) dim nextrow range sheet1.range("f7,f10,f13,f16,f19,f22,f25,f28").copy sheets("overzicht").select set nextrow = activesheet.cells(cells.rows.count, 1).end(xlup). _ offset(1, 0) nextrow.select selection.pastespecial (xlvalues), transpose:=true msgbox "invoer opgeslagen" application.cutcopymode = false application.screenupdating = true else msgbox "not cells filled! cant copy" end if end sub
update
, from comments:
yes, it's possible execute different checks individually too, example:
dim allfilled boolean if not isempty(range("f7, f10, f13, f16")) , isempty(range("f8")) ' f7, f10, f13, f16 not empty , f8 empty allfilled = true elseif isempty(range("f28")) ' f28 empty cannot execute copy-paste allfilled = false else allfilled = false end if
Comments
Post a Comment