I’m using some custom software (not excel) that’s build in VBA, there the usual VBA editor to add in custom code, can record macros etc. and all the usual VBA commands seem to work for me.
I’m trying to make a macro that plots a certain graph. I’ve recorded a macro of making the plot, got some code and edited it for what I want and got the following which is working inside a sub:
With ModelResults.Prediction
.StandardErrorMultiple = 0
.PredictedValueStdErrType = embPercentile
.ExposurePlot = embSmall
.PlotFittedValues2 = -1
.ReScale = False
.AllLevelIndex = 0
.SecondaryFactorFlag(1) = False
.SecondaryFactorFlag(2) = False
.SecondaryFactorFlag(3) = False
.SecondaryFactorFlag(4) = False
.SecondaryFactorFlag(5) = False
.SecondaryFactorFlag(6) = False
.SecondaryFactorFlag(7) = False
.SecondaryFactorFlag(Interacting_factor_index) = True
.SecondaryFactorFlag(Selected_Factor_index) = True
.MainFactorIndex2 = Selected_Factor_index
.Display
End With
The True / False flag is setting whether to plot that factor on the chart.
The problem is that as I work in this software the number of these SecondaryFactors keeps increasing so I need to keep manually adding extra:
.SecondaryFactorFlag(i) = False
lines in to keep only getting the two factors I want on the chart on there (it is basically excluding factor # i from the graph).
I’m trying to get this to work with an input of the total number needed (n) and just run. I can calculate the n fine, but using that with a For loop inside a With statement is not working:
With ModelResults.Prediction
.StandardErrorMultiple = 0
.PredictedValueStdErrType = embPercentile
.ExposurePlot = embSmall
.PlotFittedValues2 = -1
.ReScale = False
.AllLevelIndex = 0
For i = 1 To n
.SecondaryFactorFlag(i) = False
Next i
.SecondaryFactorFlag(Interacting_factor_index) = True
.SecondaryFactorFlag(Selected_Factor_index) = True
.MainFactorIndex2 = Selected_Factor_index
.Display
End With
Is it possible to get a For loop within a With statement in VBA?
Or any other ideas on how to achieve this.
I’ve tried just adding lots of these:
.SecondaryFactorFlag(i) = False
lines in but it errors when it gets to an i > the number of SecondaryFactors that exist. So I’ve tried getting to to move to the next line on an error but that then doesn’t run the whole with statement. I’ve then tried the same but splitting it out into each line being separate (so without the With statement) which allows it to move past the erroring lines but that doesn’t plot the chart (presume it needs the whole of the With statement to plot the chart.
Thanks in advance.
2