How do I code better way instead of coding like this VBA

后端 未结 1 1152
执念已碎
执念已碎 2021-01-26 14:14

I am creating a dash board in excel but however i would like to know if there is a better way of coding rather than this. I would like to moduleriz it instead of doing this to m

1条回答
  •  盖世英雄少女心
    2021-01-26 14:20

    Well, just purely based on this particular piece of code of yours, you could try something along the following lines:

    Option Explicit
    
    Sub Sample()
    
    Dim rng As Range
    Dim x As Long
    Dim arr As Variant
    
    With Sheet1
    
        'Input static values
        .Cells(35, "C").Value = .[C57]
    
        'Prepare for dynamic values
        Set rng = .Range("D35:H35")
        arr = Array(1.3, 1.02, 1.9, 1.9, 1.5)
    
        'Insert dynamic values
        For x = 1 To rng.Cells.Count
            rng.Cells(1).Value = WorksheetFunction.Round(rng.Cells(1).Value * arr(x - 1), 0)
        Next x
    
    End With
    
    End Sub
    

    Or, if your concatenated string won't go over 255 characters:

    Sub Sample()
    
    Dim str As String
    
    With Sheet1
    
        'Input static values
        .Cells(35, "C").Value = .[C57]
    
        'Prepare for dynamic values
        str = "{1.3,1.02,1.9,1.9,1.5}"
    
        'Insert dynamic values
        .Range("D35:H35").Value = .Evaluate("D35:H35*" & str)
    
    End With
    
    End Sub
    

    0 讨论(0)
提交回复
热议问题