Excel VBA为指定单元格提供数字,避免选择

标签 excel vba optimization

我在 Excel 中有以下 VBA 代码。它是很长的 VBA 的一部分,负责许多进程。如何摆脱 Select 命令?我尝试了几件事都没有成功。

ActiveWorkbook.Sheets("Installation").Select
    For Each Cl14 In ThisWorkbook.Sheets("Installation").Range("G2", Range("G" & Rows.Count).End(xlUp))
      Select Case LCase(Cl14.Value)
         Case "main"
            m14 = m14 + 1: s14 = 0: ss14 = 0
            Cl14.Offset(, -6).Value = m14
         Case "sub"
            s14 = s14 + 1: ss14 = 0
            Cl14.Offset(, -6) = m14 & "." & s14
         Case "sub-sub"
            ss14 = ss14 + 1
            Cl14.Offset(, -6) = m14 & "." & s14 & "." & ss14
      End Select
   Next Cl14

最佳答案

可以做一个强制循环来防止选择案例。但我个人更喜欢使用选择案例。

For i = 2 To .Range("G" & .Rows.Count).End(xlUp).Row
If .Range("G" & i).Value = "main" Then
'code here
ElseIf .Range("G" & i).Value = "sub" Then
'code here
ElseIf .Range("G" & i).Value = "sub-sub" Then
'code here
End If
Next i

关于Excel VBA为指定单元格提供数字,避免选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53371264/

相关文章:

javascript - 针对移动/低速连接进行优化 - 全背景视频

javascript - 将字节顺序标记添加到通过 PapaParse 解析的 CSV

excel - VBA 触发器输入或更新查找值

vba - 使用 VBA 查找

vba - Excel图表轴刻度

excel - 用于根据过滤行从选项卡复制数据的宏

algorithm - 最小最大匹配问题

c - 分支优化

excel - 使用 Microsoft Excel 绘制 CDF 图表

vba - 单击按钮后如何返回到单元格 A1?