自动填充列

时间:2015-04-26 06:45:52

标签: excel vba excel-vba

我刚刚开始使用VBA。只是想知道如何从范围(" K5")开始填充Column,以及从 ComboBox1 获取的值。例如:如果范围(" K5")= 22 范围(" K6")= 23 ...最终number是 ComboBox2 的值。

这是我现在的代码......

Dim temperature_1 As Integer                     'from temperature_1
Dim temperature_2 As Integer                     'to temperature_2
Dim k As Integer
Dim l As Integer
Dim range_degrees As Integer                      


temperature_1 = Sheet1.ComboBox1
temperature_2 = Sheet1.ComboBox2
range_degrees = temperature_2 - temperature_1

For k = range("K5") To range_degrees                     
    For l = temperature_1 To temperature_2                     
     ......
    Next l
Next k    

2 个答案:

答案 0 :(得分:3)

如果您真的希望“自动填充”该列,我只需填充该范围的开头并填写一系列。

with activesheet
    .range("K5") = temperature_1
    .range("K5").DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1, Stop:=temperature_2, Trend:=False
end with

如果要增加0.05°的增量,请将Step:=1参数更改为所需的增量。如果您想填充一行而不是填充一列,请将Rowcol:=xlColumns更改为Rowcol:=xlRows。使用系列阻止这样的值比循环它们更有效。

答案 1 :(得分:0)

Private Sub FillRange()
    Dim temperature_1 As Integer                     'from temperature_1
    Dim temperature_2 As Integer                     'to temperature_2
    Dim k As Integer
    Dim l As Integer
    Dim range_degrees As Integer


    temperature_1 = Sheet1.ComboBox1
    temperature_2 = Sheet1.ComboBox2
    range_degrees = temperature_2 - temperature_1

    Range("K5").Select

        For l = temperature_1 To temperature_2
           ActiveCell.Value = l
           ActiveCell.Offset(1, 0).Select
        Next l
End Sub