【问题标题】:"out of memory". How can I write better VBA codes instead of hard coding them?“记不清”。如何编写更好的 VBA 代码而不是硬编码?
【发布时间】:2020-07-22 02:05:07
【问题描述】:

有没有办法写一个for循环以升序方式改变单元格行以及变量Mat1到Mat2?

.Cells(4, 30) = frmProjectCost.Mat1.Value
.Cells(5, 30) = frmProjectCost.Mat2.Value
.Cells(6, 30) = frmProjectCost.Mat3.Value
.Cells(7, 30) = frmProjectCost.Mat4.Value
.Cells(8, 30) = frmProjectCost.Mat5.Value
.Cells(9, 30) = frmProjectCost.Mat6.Value
.Cells(10, 30) = frmProjectCost.Mat7.Value
.Cells(11, 30) = frmProjectCost.Mat8.Value
.Cells(12, 30) = frmProjectCost.Mat9.Value
.Cells(13, 30) = frmProjectCost.Mat10.Value
.Cells(14, 30) = frmProjectCost.Mat11.Value
.Cells(15, 30) = frmProjectCost.Mat12.Value
.Cells(16, 30) = frmProjectCost.Mat13.Value
.Cells(17, 30) = frmProjectCost.Mat14.Value
.Cells(18, 30) = frmProjectCost.Mat15.Value

【问题讨论】:

标签: excel vba


【解决方案1】:

尝试类似:

Option Explicit

Private Sub FormLoop()

    Dim Counter As Long
    
    For Counter = 1 To 15
        ActiveSheet.Cells(Counter + 3, 30) = frmProjectCost.Controls("Mat" & Counter).Value
    Next

End Sub

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多