excel - Adding Validation List with VBA is unstable -


in setws sheet have following code in worksheet_deactivate:

private sub worksheet_deactivate() dim actws, setws  worksheet     set actws = activeworkbook.sheets("activity_plan")     set setws = activeworkbook.sheets("settings") actws.range("j11:j20").validation     .delete     .add type:=xlvalidatelist, alertstyle:=xlvalidalertstop, _                 operator:=xlbetween, formula1:="=settings!$as$10:$as$20"                                  '     .ignoreblank = true     .incelldropdown = true end                                                                                             ' end sub 

in repws sheet (where create couple of graphs) have following code in worksheet_activate:

private sub worksheet_activate() application.enableevents = false application.screenupdating = false application.calculation = xlcalculationmanual dim scopews, repws, actws, setws worksheet     set scopews = activeworkbook.sheets("scope")     set repws = activeworkbook.sheets("rep")     set actws = activeworkbook.sheets("activity_plan")     set setws = activeworkbook.sheets("settings") lrowscopee = scopews.range("e" & rows.count).end(xlup).row if setws.range("w17") > setws.range("w18")                                                 '     msgbox ("bla bla")     exit sub end if repws.chartobjects("diagramm 3").activate activechart.seriescollection(1).name = "=scope!$m$4" activechart.seriescollection(1).values = "=scope!$m$11:$m$" & lrowscopee activechart.seriescollection(1).xvalues = "=scope!$e$11:$e$" & lrowscopee activechart.seriescollection(2).name = "=scope!$p$4" activechart.seriescollection(2).values = "=scope!$p$11:$p$" & lrowscopee activechart.seriescollection(3).name = "=scope!$u$4" activechart.seriescollection(3).values = "=scope!$t$11:$t$" & lrowscopee activechart.axes(xlvalue).maximumscaleisauto = true activechart.axes(xlvalue).ticklabels.numberformat = "#.##0 €" activechart.fullseriescollection(1).datalabels.numberformat = "#.##0 €"  activesheet.chartobjects("diagramm 14").activate activechart.seriescollection(1).name = "=settings!$cj$10" activechart.seriescollection(1).values = "=settings!$cj$11:$cj$" & setws.range("cl8").value activechart.seriescollection(1).xvalues = "=settings!$ci$11:$ci$" & setws.range("cl8").value activechart.seriescollection(2).name = "=settings!$ck$10" activechart.seriescollection(2).values = "=settings!$ck$11:$ck$" & setws.range("cl8").value application.enableevents = true application.calculation = xlcalculationautomatic application.screenupdating = true end sub 

when switch setws repws, throws error

"application defined or object defined error"

and highlights in setws following:

.add type:=xlvalidatelist, alertstyle:=xlvalidalertstop, _                 operator:=xlbetween, formula1:="=settings!$as$10:$as$20" 

switching between other pair of sheets in file not cause error (e.g. switching setws other sheet ok).

update: notice more thing - activate repws once, further attempt switch setws repws throws error. wrong repws code...

avoid use of active(workbook/sheet/cell/chart/...), .activate/.select method , .selection property.

your worksheet_activate sub, might this

private sub worksheet_activate()     dim scopews, repws, actws, setws worksheet      application         .enableevents = false         .screenupdating = false         .calculation = xlcalculationmanual     end      thisworkbook         set scopews = .sheets("scope")         set repws = .sheets("rep")         set actws = .sheets("activity_plan")         set setws = .sheets("settings")     end      lrowscopee = scopews.range("e" & rows.count).end(xlup).row     if setws.range("w17") > setws.range("w18")                                                 '         msgbox ("bla bla")     else         repws         'diagram 3             .chartobjects("diagram 3").chart             'series 1                 .seriescollection(1)                     .name = "=scope!$m$4"                     .values = "=scope!$m$11:$m$" & lrowsco                     .xvalues = "=scope!$e$11:$e$" & lrowscopee                 end             'series 2                 .seriescollection(2)                     .name = "=scope!$p$4"                     .values = "=scope!$p$11:$p$" & lrowscopee                 end             'series 3                 .seriescolection(3)                     .name = "=scope!$u$4"                     .values = "=scope!$t$11:$t$" & lrowscopee                 end             'layout                 .axes(xlvalue)                     .maximumscaleisauto = true                     .ticklabels.numberformat = "#.##0 €"                 end                 .fullseriescollection(1).datalabels.numberformat = "#.##0 €"             end         'diagram 14             .chartobjects("diagram 14").chart             'series 1                 .seriescollection(1)                     .name = "=settings!$cj$10"                     .values = "=settings!$cj$11:$cj$" & setws.range("cl8").value                     .xvalues = "=settings!$ci$11:$ci$" & setws.range("cl8").value                 end             'series 2                 .seriescollection(2)                     .name = "=settings!$ck$10"                     .values = "=settings!$ck$11:$ck$" & setws.range("cl8").value                 end             end         end     end if      application         .enableevents = true         .calculation = xlcalculationautomatic         .screenupdating = true     end end sub 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -