VBA For loop inside a With statement [closed]

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.SecondaryFactorFlag(i) = False
</code>
<code>.SecondaryFactorFlag(i) = False </code>
.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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.SecondaryFactorFlag(i) = False
</code>
<code>.SecondaryFactorFlag(i) = False </code>
.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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật