为行中的非空单元格创建一个均匀间隔数字的数组

时间:2017-12-15 14:46:58

标签: excel vba

我正在尝试在宏中编写代码。

对于范围内的每一行:

  1. 我试图连续选择非空单元格。

  2. 对于这些单元格,请选择最小值n_1

  3. 给定乘法因子a创建一个从最小值开始的等间隔数字的数组(与非空行长度相同),即(n_k = (a^k)*n_1)。

  4. 沿着这些方向的东西

    Dim a As Range, b As Range, number_of_elements as Integer
    
    Set a = Range()
    
    For Each b In a.Rows
        Dim newarray as Variant 'initialize new array
    
        arr = select_non_empty_cells(b) 'select non empty cells
        number_of_elements = Ubound(arr) 'get number of elements
    
        ReDim newarray(1 To number_of_elements) As Integer 'set the dimension
    
        min_val = WorksheetFunction.Min(arr.Value) 'pick minimum value
    
        For counter = 1 To number_of_elements 'create new array with equally spaced numbers
          newarray(counter) = min_val*1.25^counter 'multiplying factor
        Next counter
    
        arr.Value = newarray.Value 'set the non empty range to new values
    Next
    

    以下是我的数据的样子。因此,对于第一行,我将选择1033.2(最小值)并创建具有均匀间隔的相同长度的5个元素的新数组。第二行也是如此。

    enter image description here

1 个答案:

答案 0 :(得分:1)

也许是这样的:

Sub Korba()
    Dim i As Long, mini As Long
    Dim WhichRow As Long
    Dim factr As Double

    mini = 3
    factr = 1.25
    WhichRow = 5

    For i = 1 To Columns.Count
        With Cells(WhichRow, i)
            If .Value <> "" Then Exit Sub
            .Value = mini * factr ^ i
        End With
    Next i
End Sub

enter image description here